I can access a MySQL database and store output to an R dataframe using the following script where sam_pn = walker
con <- dbConnect(MySQL(),
user = user,
password = password,
host = host,
dbname = dbname)
df = dbGetQuery(con, "SELECT *
FROM sam AS s
JOIN che AS c ON c.che_label = s.sam_label1
WHERE sam_pn = 'walker'")
But what i would like to do is store 'walker' as an R value pn and then use pn value in the sql query like below so i can vary the pn value.... but it does not work. The syntax is not right. Note sam and che are tables in the database
pn = 'walker'
df = dbGetQuery(con, "SELECT *
FROM sam AS s
JOIN che AS c ON c.che_label = s.sam_label1
WHERE sam_pn = 'pn'")
pn = 'walker'
df = dbGetQuery(con, "SELECT *
FROM sam AS s
JOIN che AS c ON c.che_label = s.sam_label1
WHERE sam_pn = ?",
params = list(pn))
This is what worked in the end
pn = 'walker'
data = dbGetQuery(con, paste0("SELECT *
FROM sam AS s
JOIN che AS c ON c.che_label = s.sam_label1
WHERE sam_pn = '", pn ,"'"))
For my toggle of a Boolean value with Flask and SQLite I want to change 1 into 0 and 0 into 1 on table engineering_project. The message can be successfully flashed so the if function is working. However, the value of engg_proj_status in the table cannot be updated:
conn = sqlite3.connect(db_path)
conn.row_factory = sqlite3.Row
c = conn.cursor()
c.execute("SELECT engg_proj_status FROM engineering_project WHERE engg_proj_id =?",(engg_proj_id,))
status = c.fetchone()[0]
if status == 1:
c.execute("UPDATE engineering_project SET engg_proj_status = ? WHERE engg_proj_id = ?;",(False,engg_proj_id))
flash("Status changed from COMPLETED to OPEN")
else:
c.execute("UPDATE engineering_project SET engg_proj_status = ? WHERE engg_proj_id = ?;",(True,engg_proj_id))
flash("Status changed from OPEN to COMPLETED")
Add conn.commit()
Thanks to this comment from forpas.
SQL and r ”update fields”
Can anyone help Me.? i am trying to update fields in a row in a table on a sql server.
I have tried to send a vector with that contains the data I want to update(new_d), but I get an error every time I try to send the object with sql query. If I hardcode it, it works fine and the update goes through to the server.
what goes wrong when it does not want to send the object and what does it take to make it work? so it can save the update to the server
Hope anyone can show me what i do worng.
library(tidyverse)
library(readxl)
library(DT)
library(RODBC)
library(DBI)
library(odbc)
library(dbplyr)
# Connection is working ---------------------------------------------------
conn <- dbConnect(odbc(),
Driver = "ODBC Driver 13 for SQL Server",
Server = "IP, PORT",
Database = "DBNAME",
UID = "USERNAME",
PWD = "PASSWORD")
new_d <- c(name = 'Rap',
dateOfBirth = '1938-06-06',
city = 'Hollywood',
gender = 'Man',
time() = 8,
feeling = 'Happy')
# NOT WORKING -------------------------------------------------------------
update_statement1 <- build_sql("UPDATE ShinyTest
SET Name =?, City =?, Gender =?,
DateOfBirth =?, Time =?, Feeling =?
WHERE ShinyUserID = 4", new_d, con = conn)
update_statement1
# NOT WORKING -------------------------------------------------------------
update_statement2 <- build_sql("UPDATE ShinyTest
SET Name = name, City = city, Gender = gender,
DateOfBirth = dateOfBirth, Time = time, Feeling = feeling
WHERE ShinyUserID = 4", new_d, con = conn)
update_statement2
# NOT WORKING -------------------------------------------------------------
update_statement3 <- build_sql("UPDATE ShinyTest
SET Name = 'name', City = 'city', Gender = 'gender',
DateOfBirth = 'dateOfBirth', Time = 'time', Feeling = 'feeling'
WHERE ShinyUserID = 4", new_d, con = conn)
update_statement3
# NOT WORKING -------------------------------------------------------------
update_statement4 <- build_sql("UPDATE ShinyTest
SET Name = $name, City = $city, Gender = $gender,
DateOfBirth = $dateOfBirth, Time = $time, Feeling = $feeling
WHERE ShinyUserID = 4", new_d, con = conn)
update_statement4
# WORKING -------------------------------------------------------------
update_statement5 <- build_sql("UPDATE ShinyTest
SET Navn = 'Rap', City = 'Hollywood', Gender = 'Mand',
DateOfBirth = '1938-06-06', Tid = '8', Feeling = 'Glad'
WHERE ShinyUserID = 4", con = conn)
update_statement5
# NOT WORKING -------------------------------------------------------------
DBI::dbSendQuery(conn, update_statement1)
DBI::dbSendQuery(conn, update_statement2)
DBI::dbSendQuery(conn, update_statement3)
DBI::dbSendQuery(conn, update_statement4)
# WORKING -------------------------------------------------------------
DBI::dbSendQuery(conn, update_statement5)
DBI::dbDisconnect(conn)
# The Error i get ---------------------------------------------------------
#Error: nanodbc/nanodbc.cpp:1655: 42000: [Microsoft][ODBC Driver 13 for SQL Server][SQL Server]Incorrect syntax near 'Rap'. [Microsoft][ODBC Driver 13 for SQL Server][SQL Server]Statement(s) could not be prepared.
#<SQL> 'UPDATE ShinyTest
# SET Navn = navn, City = city, Gender = gender,
# DateOfBirth = dateOfBirth, Tid = tid, Feeling = feeling
# WHERE ShinyUserID = 4('Rap', '1938-06-06', 'Hollywood', 'Mand', '8', 'Glad')'
from tkinter import *
import pyodbc
with pyodbc.connect("DSN=ffff", autocommit=True) as conn:
df2 = pd.read_sql("SELECT * FROM holdingsummarysaaas", conn)
list46 = []
for md in df2['clientcode']:
list46.append(md)
print(list46)
result = []
for i in list46:
if i not in result:
result.append(i)
print(result)
#list_no_nan = [x for x in result if pd.notnull(x)]
#print(list_no_nan)
#select name from studens where id in (%s)" % ",".join(map(str,mylist)
#def fun():
num1 = Entry(root)
blank = Entry(root)
Ans = (num1.get())
blank.insert(0, Ans)
if Ans in result:
df2 = pd.read_sql('SELECT * FROM holdingsummary where ' + ' or '.join(('clientcode = '
+ str(n) for n in result)),conn)
#df2 = pd.read_sql("SELECT * FROM holdingsummary where clientcode = '" + Ans + "'",
conn)
print(df2)
I am taking unique clientcodes from database and I want user input which is in fun
function should access the results list(unique clientcode )check each clientcode from list
and check whether entered clientcode if same in result list then open that clientcode.
for eg:
Ans=='100014'
#df2 = pd.read_sql("SELECT * FROM holdingsummarysaaas where clientcode = '100014', conn)
class Assets:
def __init__(self):
conn = sqlite3.connect('FastFood.db')
c = conn.cursor()
self.Cash = input('Do you want to enter Cash:')
self.Equipment = input('Do you want to enter Eqiupment:')
self.Supplies = input('Do you want to enter Supplies:')
self.PrepaidInsurance = input('Do you want to enter Prepaid Insurance:')
self.ARecievale = input('Do you want to enter Accounts Recievable :')
query = c.execute('INSERT INTO Financial_Table (Account_Title)VALUES', (self.Cash))
result = conn.execute(query)
conn.close()
''' I am tring to insert entry into one column but it's giving a syntax error.'''
'''We have total 4 columns'''