Syntax for changing a python script from sqlite3 to mysql - python

I have a working python script which retrieves data from a sqlite3 db. I need to convert it to talk to a mysql db though, and here's the first line that is giving me grief:
PRODUCT_CODE_ID = "SELECT id FROM localsite_productcode WHERE localsite_productcode.product_code=?"
cur.execute(PRODUCT_CODE_ID,(product_code,))
When I try that with mysql I get the following error:
Traceback (most recent call last):
File "gen-csv.py", line 85, in <module>
cur.execute(PRODUCT_CODE_ID,(product_code,))
File "/usr/lib/pymodules/python2.6/MySQLdb/cursors.py", line 151, in execute
query = query % db.literal(args)
TypeError: not all arguments converted during string formatting
What am I doing wrong here?

I don't think the DB API for MySQL supports the ? as a place holder - just try %s instead.

Related

Trying to create a query using python and mariadb

Sorry, this may be really easy but I am new at learning code,
I am trying to return all
yesterday = ((dt.date.today()-dt.timedelta(days=1)))
cur.execute("SELECT * FROM iron_condor WHERE open='yes' AND DATE(buy_date)<DATE(?)"(yesterday))
I am getting this back
Traceback (most recent call last):
File "/home/pi/Documents/openoptions.py", line 86, in <module>
cur.execute("SELECT * FROM iron_condor WHERE open='yes' AND DATE(buy_date)<DATE(?)"(yesterday))
TypeError: 'str' object is not callable
yesterday = ((dt.date.today()-dt.timedelta(days=1)))
cur.execute("SELECT * FROM iron_condor WHERE open='yes' AND DATE(buy_date)<DATE(?)", (yesterday,))
note the 2 commas. You may need to convert yesterday to str.

MySQL Type Converter Issue

I am new to tkinter and I am trying to connect python with MySQL. When I Try to insert the records via a new window in python I get the following error message
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Users\sapna\anaconda3\lib\tkinter\__init__.py", line 1705, in __call__
return self.func(*args)
File "C:\Users\sapna\Desktop\SAMARTH.py", line 141, in dynamic_data_entry
libcur.execute("INSERT INTO tcl VALUES(%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)" ,
(id1,name1,auth1,doi1,dor1,reiss,rname,Address,gen1,mil1))
File "C:\Users\sapna\anaconda3\lib\site-packages\mysql\connector\cursor_cext.py", line 248, in
execute
prepared = self._cnx.prepare_for_mysql(params)
File "C:\Users\sapna\anaconda3\lib\site-packages\mysql\connector\connection_cext.py", line 626, in
prepare_for_mysql
result = self._cmysql.convert_to_mysql(*params)
_mysql_connector.MySQLInterfaceError: Python type method cannot be converted
Here is the snip of my insert command.
def dynamic_data_entry():
bname.delete(0,END)
bid.delete(0,END)
author.delete(0,END)
DOI.delete(0,END)
DOR.delete(0,END)
RES.delete(0,END)
RNM.delete(0,END)
AD.delete(0,END)
mail.delete(0,END)
id1=ln.get()
name1=fn.get()
auth1=dn.get()
doi1=an.get()
dor1=gn.get()
rname=kn.get()
Address=pn.get
reiss=mn.get()
gen1=var.get()
mil1=ll.get()
libcur.execute("INSERT INTO tcl VALUES(%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)" ,
(id1,name1,auth1,doi1,dor1,reiss,rname,Address,gen1,mil1))
mydb.commit()
messagebox.showinfo("INSERT STATUS","RECORD HAS BEEN ADDED SUCCESSFULLY")
libcur.close()
The datatype that I have used is varchar() in Book_id,DOI,DOR and char() in others for MySQL.
All the dataypes that I have used in Python are as follows
var=StringVar()
fn=StringVar()
yn=StringVar()
ln=StringVar()
dn=StringVar()
an=StringVar()
kn=StringVar()
gn=StringVar()
mn=StringVar()
pn=StringVar()
ll=StringVar()
Thanks in advance
Looks like you missed some parentheses in your assignment to Address. Try Address=pn.get().
The error implies that you're passing a method rather than a value. This is consistent with Address=pn.get, i.e. you're passing the pn.get method rather than the return value of pn.get().

Error with Sqllite3 reading .sql file

So I've got a .sql file that I want to build over to a .db file for sqllite3, but I'm getting a
sqlite3.OperationalError: near "ENGINE": syntax error
Any ideas why? Any help is appreciated.
import sqlite3
conn = sqlite3.connect('newdatabase.db')
f = open('olddatabase.sql','r')
sql = f.read()
conn.executescript(sql)
conn.close()
OUTPUT:
Traceback (most recent call last):
File "deleteme.py", line 6, in
conn.executescript(sql)
sqlite3.OperationalError: near "ENGINE": syntax error
(program exited with code: 1)
Divining based on ENGINE, you have a .sql file dumped by MySQL (CREATE TABLE ... ENGINE=InnoDB or similar), so it contains MySQL-specific extensions.
You'll have to either edit the .sql file to conform to SQLite's standards or figure out a way to convert it automatically to a more standard sequence of SQL instructions.

MySQL and TypeError: not enough arguments for format string

I have a trouble with my program in my raspberry pi. This is my source code
import MySQLdb
import csv
db=MySQLdb.connect(user='root',passwd='toor',
host='127.0.0.1',db='data')
cursor=db.cursor()
csv_data=csv.reader(file('datasensor.txt'))
for row in csv_data:
sql = "insert into `kelembapan` (`id`,`Tanggal`,`Tipe_sensor`,`Value`,`Ket`) values(%s,%s,%s,%s,%s);"
cursor.execute(sql,row)
db.commit()
cursor.close()
print "The Data has been inputted"
and this is for txt file
1, 2017-10-10, sensor1,40,Kurang lembap
2, 2017-10-10, sensor2,60,Lembap
That program can run in my ubuntu but not in my raspberry. when run in raspberry there is error
Traceback (most recent call last):
File "server.py", line 9, in <module>
cursor.execute(sql,row)
File "/usr/lib/python2.7/dist-packages/MySQLdb/cursors.py",line 159, in execute
query = query% db.literal(args)
TypeError: not enough arguments for format strings
Thnks before :)

[Python]:phoenixdb.errors.ProgrammingError: ("Syntax error. Unexpected char: '!'", 601, '42P00', None)

I am using a python module called phoenixdb to access phoenix which is SQL wrapper to query over HBase.
Here is my code snippet:-
import phoenixdb
database_url = 'http://localhost:8765/'
conn = phoenixdb.connect(database_url, autocommit=True)
cursor = conn.cursor()
cursor.execute("!table")
print cursor.fetchall()
cursor.close()
The phoenix query to list all the schemes and tables is !table or !tables.
But when I try passing the same in the execute function as shown above, I get the following error:-
Traceback (most recent call last):
File "phoenix_hbase.py", line 7, in <module>
cursor.execute("!table")
File "build/bdist.linux-x86_64/egg/phoenixdb/cursor.py", line 242, in execute
File "build/bdist.linux-x86_64/egg/phoenixdb/avatica.py", line 345, in prepareAndExecute
File "build/bdist.linux-x86_64/egg/phoenixdb/avatica.py", line 184, in _apply
File "build/bdist.linux-x86_64/egg/phoenixdb/avatica.py", line 90, in parse_error_page
phoenixdb.errors.ProgrammingError: ("Syntax error. Unexpected char: '!'", 601, '42P00', None)
Funny part is when I try to passing a different query, for example a select query, then script gets executed and produces result just fine.
Query:cursor.execute("select * from CARETESTING.EDR_BIN_SOURCE_3600_EDR_FLOW_SUBS_TOTAL limit 1")
Result:
[[2045,1023,4567]]
Is there any other format for passing !table which is equivalent to show tables in phoenixdb library's execute function which I am missing?
I tried looking up on the internet but unfortunately haven't come across anything helpful so far.
Thanks
!tables is sqlline grammar that can not be parsed by JDBC interface.

Categories