Importing the SQLite3 module works at first, and creating a database or table works fine too.
But suddenly, when trying to insert something in it — Python throws an irresponsible kind of error (sometimes throws syntaxError without highlighting any line of code — seriously, there's no reason for the error).
I know SQLite3 on Android 6.0 raises issues — there are many options to solve this on Java but is there any solution for QPython (Android 6.0)?
This is my insert line:
c.execute("INSERT INTO pins(pin) VALUES (?)", (S)
The above is the line python points the error to
Close the cur.execute properly
There is this thing with python tuples, so add a comma to the data tuple
cur.execute("INSERT INTO pins(pin) VALUES (?)", (S,))
Related
I've been having this issue for some time now and here is the short story:
I want to edit a date in my Sqlite database with a Python script so I do this using:
import sqlite3
db = sqlite3.connect('test.db')
cursor = db.cursor()
cursor.execute("UPDATE info SET date = '2003-03-14 12:34:20' WHERE id = 10")
but when I execute this I get an exception:
sqlite3.OperationalError: no such module: fts4
with some troubleshooting I've managed to narrow it down to having some issues with the quote marks. Since even if I just reduce it to a single year it still produces an exception.
The same exception appear if I try to insert a string using quote marks.
How can I fix this? Most solutions I find is solved though enabling fts3/fts4 with the SQLITE_ENABLE_FTS3 flag when building, but I have no idea of how to do that.
Besides, the exact same code runs smoothly both in Python 3.6.5 and 2.7.12. what happened in Python3?, or more specifically 3.5.2.
I should add, that it works in linux but not in windows.
I have a simple script that downloads data out of one database (Teradata), does some stuff to it, and uploads it into another (MySQL) database. This has worked well for months now, but yesterday in my logs I noticed that the script failed, and gave me back this error:
An error has occurred:
(<class '_mysql_exceptions.ProgrammingError'>, ProgrammingError(1064,
'You have an error in your SQL syntax; check the manual that
corresponds to your MySQL server version for the right syntax to use
near \'A435", NULL, "2018-01-18", 95,\' at line 1'), <traceback object
at 0x00000000019A1A48>)
It seems like the culprit may be one of the user-editable fields, although each of these fields is processed by mysqli_real_escape_string in PHP prior to writing to the database - so not sure there.
While it would be nice from a programming standpoint to understand exactly what happened here, I'm more concerned with editing the python script to include an error handler that just skips over any line that causes an error instead of exiting the entire script.
Here's the script:
# Upload this to MySQL
db = MySQLdb.connect(host='asdf',user='asdf',passwd='asdf',db='asdf')
cursor = db.cursor()
csv_data = csv.reader(file(csvfilename))
for row in csv_data:
cursor.execute('INSERT INTO `test` (field_1,field_2,field_3,field_4,field_5)'\
'VALUES(%s,%s,%s,%s,%s)',row);
db.commit()
# Close the connection to the database.
cursor.close()
When use MySQLdb package, triple quotes are used for quoting query string:
Document from link: http://mysql-python.sourceforge.net/MySQLdb.html
db.query("""SELECT spam, eggs, sausage FROM breakfast WHERE price < 5""")
In your case, it might coincidence that you have both single quote and double qutoe in the parsed line and then the script crashes the insertion execution.
I am a beginner programmer (actually automated QA tester) working on an internal project (specific to my work) where I am trying to use a UDF (external function aka User Defined Function) that is stored in a Firebird database (I hope I phrased all that correctly).
I'm interfacing to the Firebird database using the fdb driver for Python, I can connect, run basic SQL statements, but when I run the following statement the UDF returns 0 instead of the expected value.
cur.execute("INSERT INTO temp (column1,column2) VALUES(f_udfName(?),?)",(int(line.rstrip('\n')),line.rstrip('\n')))
I'm traversing through a file and each line of the file is an 11-digit number. I am wanting to insert the original number into column1 and the return value of the UDF into column2 of the table temp. When I hit this line of code it completes successfully, but I get a return value of 0 instead of 9-digit number.
I have googled everything I could think of to figure this out. If someone would be able to point me in the correct direction at least, I would greatly appreciate it.
I am using Python 3.4, most recent fdb version, and Firebird 2.5.
EDIT: If I run the code from the python command line and do not parametrize the statement the UDF returns the expected value. Example:
cur.execute("INSERT INTO temp (column1,column2) VALUES(f_udfName(12345678901),12345678901)")
As recommended by Mark Rotteveel, changing the SQL statement to CAST the parameter as a BIGINT resolved the problem.
cur.execute("INSERT INTO temp (column1,column2) VALUES(f_udfName(CAST(? AS BIGINT)),?)",(int(line.rstrip('\n')),line.rstrip('\n'))
Attempting to use the long(..) function in Python 3.4 did not work because as of Python 3.0.1 the long(..) function was merged into the int(..) function. Attempting to use int(..) will result in a return value of 0 without the CAST statement.
For the record, I have looked into this, but cannot seem to figure out what is wrong.
So I'm doing the tutorial on web.py, and I get to the database part (can do everything above it). I wanted to use sqlite3 for various reasons. Since I couldn't figure out where to type the
sqlite3 test.db
line, I look into the sqlite3 module, and create a database with that. The code for that is:
import sqlite3
conn = sqlite3.connect("test.db")
print("Opened database successfully");
conn.execute('''CREATE TABLE todo
(id serial primary key,
title text,
created timestamp default now(),
done boolean default 'f');''')
conn.execute("INSERT INTO todo (title) VALUES ('Learn web.py')");
but I get the error
done boolean default 'f');''')
sqlite3.OperationalError: near "(": syntax error
I've tried looking into this, but cannot figure out for the life of me what the issue is.
I haven't had luck with other databases (new to this, so not sure on the subtleties), I wasn't able to just make the sqlite database directly so it might be a python thing, but it matches the tester.py I made with the sqlite with python tutorial...
Thanks if anyone can help me!
The problem causing the error is that you can't use the MySQL now() function here. Try instead
created default current_timestamp
This works:
conn.execute('''CREATE TABLE todo
(id serial primary key,
title text,
created default current_timestamp,
done boolean default 'f');''')
You are using SQLite but are specifying data types from some other database engine. SQLite accepts only INT, TEXT, REAL, NUMERIC, and NONE. Boolean is most likely being mapped to one of the number types and therefore DEFAULT 'F' is not valid syntax (although I don't think it would be valid in any version of SQL that does support BOOLEAN as a datatype, since they general use INTEGER for the underlying storage).
Rewrite the CREATE TABLE statement with SQLite datatypes and allowable default values and your code should work fine.
More details on the (somewhat unusual) SQLite type system: http://www.sqlite.org/datatype3.html
Question: Why is this sqlite3 statement not updating the record?
Info:
cur.execute('UPDATE workunits SET Completed=1 AND Returns=(?) WHERE PID=(?) AND Args=(?)',(pickle.dumps(Ret),PID,Args))
I'm using python and sqlite3. this statement does not throw an error, it just seems like it is out right ignored. for testing reasons I included below it:
cur.execute('SELECT * FROM workunits WHERE PID=(?) AND Args=(?)',(PID,Args))
Which returns a record just fine. but the record doesn't up date with the new value of the pickled ret. it remains u''. I can't figure out why. my where statement seems to work. my syntax seems to be correct because there is no error being thrown. I'm clueless as to why exactly it doesn't work.
If the problem persists after you fixed your syntax. Please make sure you're using:
conn.commit()
After cur.execute, UPDATES and INSERTS require COMMIT.
don't use 'AND', use a ','.
cur.execute('UPDATE workunits SET Completed=1, Returns=? WHERE PID=? AND Args=?',
(pickle.dumps(Ret), PID, Args)
)