Python SQLite3 - Table Output Format Question & Handling IntegrityError
I have a table created using Python + Sqlite3
c.execute('CREATE TABLE IF NOT EXISTS reg(col1 TEXT PRIMARY KEY, col2 TEXT)')
I even wrote a record into it using val1 and val2 variables which have string values (URLs to be specific)
c.execute('INSERT INTO reg VALUES(?,?)', (val1, val2))
Now when I print the table using this code
c.execute('SELECT * FROM reg')
rows = c.fetchall()
print rows
It shows output as
[(u'http://www.someURL.com', u'http://www.someURL.com/someDIR/')]
Now, what is this u in front of my column values mea开发者_如何转开发n?
EDIT: My next doubt is in continuation to this question so I am updating this thread instead of creating a new one. I have updated the title too. Hope that's OK.
From the above code, It is clear that my col1 is a Primary Key. So, when I tried to purposefully insert the same records for the second time it throws the exception
IntegrityError: column col1 is not unique
In order to handle this, I modified the code like this...
try:
c.execute('INSERT INTO reg VALUES(?,?)', (val1, val2))
except sqlite3.IntegrityError:
print val1+ " is already in the database"
Now executing this code caused me this exception
Traceback (most recent call last):
File "C:\Documents and Settings\user\Desktop\dbUrl.py", line 47, in <module>
c.execute('INSERT INTO reg VALUES(?,?)', (val1,val2))
OperationalError: database is locked
Any idea how do I resolve this? Infact, I don't even understand what this OperationError mean and what made the DB to be locked?
The u
means those are Unicode strings. They can be encoded to any character set you want:
u'http://www.someURL.com'.encode('ascii') # or 'latin-1' or 'utf-8'
You'll get back the same string without the u
because they're all ASCII characters.
See http://docs.python.org/howto/unicode.html.
If the strings only contain ASCII characters (all the normal symbols on an English keyboard) then you don't have to worry too much about this. If, however, you're using an extended character set, you need to make sure you're specifying the right encoding when saving strings or displaying them to the user.
Edit: When I execute
import sqlite3
con = sqlite3.connect(":memory:")
con.isolation_level = None
c = con.cursor()
val1, val2 = 'a', 'b'
c.execute('CREATE TABLE IF NOT EXISTS reg(col1 TEXT PRIMARY KEY, col2 TEXT)')
c.execute('INSERT INTO reg VALUES(?,?)', (val1, val2))
try:
c.execute('INSERT INTO reg VALUES(?,?)', (val1, val2))
except sqlite3.IntegrityError:
print val1, "is already in the database"
I get a is already in the database
. (Note the proper way to do that print statement, although yours is perfectly valid.) So your problem is somewhere else -- locked
means something else is writing to the database. Try this with an in memory database as I did, so you know for sure nothing else is accessing it. If this works, your problem is what the exception indicates -- something else is accessing the database.
That means the strings are encoded as Unicode.
精彩评论