Advanced SQLite Usage in Python
2014-03-30 13:47
357 查看
Following the SQLite3 series, this post is about some advanced topics when we are working with the SQLite3 module. If you missed the first part, you can find it here.
Sometimes we need to insert and retrieve some
in our SQLite3 database. When you execute the insert query with a date or datetime object, the
calls the default adapter and converts them to an ISO format. When you execute a query in order to retrieve those values, the
is going to return a string object:
The problem is that if you inserted a date object in the database, most of the time you are expecting a date object when you retrieve it, not a string object. This problem can be solved passing
the
Changing the connect method, the database now is returning a date object. The
type to return the correct type of object. So, if we need to work with a
column in the table as a
In case you have declared a column type as
it is necessary to modify your query in order to parse the object correctly:
Using
Sometimes we need to insert a sequence of objects in the database, the
to execute a SQL query against a sequence.
Please note that each element of the sequence must be a tuple.
The
SQL sentences you should use
If you need to read the script from a file:
Please remember that it is a good idea to surround your code with a
the exceptions. To learn more about the
Python Exceptions – The try/except/else keywords article.
Sometimes we need to use our own functions in a statement, specially when we are inserting data in order to accomplish some specific task. A good example of this is when we are storing passwords in the database and we need to encrypt those passwords:
The
name used to call the function inside the statement), the number of parameters the function expects (1 parameter in this case) and a callable object (the function itself). To use our registered function, we called it using
the statement.
Finally, PLEASE use a true encryption algorithm when you are storing passwords!
via:http://www.pythoncentral.io/advanced-sqlite-usage-in-python/
Using SQLite's date and datetime Types
Sometimes we need to insert and retrieve some dateand
datetimetypes
in our SQLite3 database. When you execute the insert query with a date or datetime object, the
sqlite3module
calls the default adapter and converts them to an ISO format. When you execute a query in order to retrieve those values, the
sqlite3module
is going to return a string object:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | >>> import sqlite3 >>> from datetime import date, datetime >>> >>> db = sqlite3.connect(':memory:') >>> c = db.cursor() >>> c.execute('''CREATE TABLE example(id INTEGER PRIMARY KEY, created_at DATE)''') >>> >>> # Insert a date object into the database >>> today = date.today() >>> c.execute('''INSERT INTO example(created_at) VALUES(?)''', (today,)) >>> db.commit() >>> >>> # Retrieve the inserted object >>> c.execute('''SELECT created_at FROM example''') >>> row = c.fetchone() >>> print('The date is {0} and the datatype is {1}'.format(row[0], type(row[0]))) # The date is 2013-04-14 and the datatype is <class 'str'> >>> db.close() |
PARSE_DECLTYPESand
PARSE_COLNAMESto
the
connectmethod:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | >>> import sqlite3 >>> from datetime import date, datetime >>> >>> db = sqlite3.connect(':memory:', detect_types=sqlite3.PARSE_DECLTYPES|sqlite3.PARSE_COLNAMES) >>> c = db.cursor() >>> c.execute('''CREATE TABLE example(id INTEGER PRIMARY KEY, created_at DATE)''') >>> # Insert a date object into the database >>> today = date.today() >>> c.execute('''INSERT INTO example(created_at) VALUES(?)''', (today,)) >>> db.commit() >>> >>> # Retrieve the inserted object >>> c.execute('''SELECT created_at FROM example''') >>> row = c.fetchone() >>> print('The date is {0} and the datatype is {1}'.format(row[0], type(row[0]))) # The date is 2013-04-14 and the datatype is <class 'datetime.date'> >>> db.close() |
sqlite3module uses the column's
type to return the correct type of object. So, if we need to work with a
datetimeobject, we must declare the
column in the table as a
timestamptype:
1 2 3 4 5 6 7 8 9 10 11 | >>> c.execute('''CREATE TABLE example(id INTEGER PRIMARY KEY, created_at timestamp)''') >>> # Insert a datetime object >>> now = datetime.now() >>> c.execute('''INSERT INTO example(created_at) VALUES(?)''', (now,)) >>> db.commit() >>> >>> # Retrieve the inserted object >>> c.execute('''SELECT created_at FROM example''') >>> row = c.fetchone() >>> print('The date is {0} and the datatype is {1}'.format(row[0], type(row[0]))) # The date is 2013-04-14 16:29:11.666274 and the datatype is <class 'datetime.datetime'> |
DATE, but you need to work with a
datetimeobject,
it is necessary to modify your query in order to parse the object correctly:
1 2 3 4 5 6 7 8 | c.execute('''CREATE TABLE example(id INTEGER PRIMARY KEY, created_at DATE)''') # We are going to insert a datetime object into a DATE column now = datetime.now() c.execute('''INSERT INTO example(created_at) VALUES(?)''', (now,)) db.commit() # Retrieve the inserted object c.execute('''SELECT created_at as "created_at [timestamp]" FROM example''') |
as "created_at [timestamp]"in the SQL query will make the adapter to parse the object correctly.
Insert Multiple Rows with SQLite's executemany
Sometimes we need to insert a sequence of objects in the database, the sqlite3module provides the
executemanymethod
to execute a SQL query against a sequence.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | # Import the SQLite3 module import sqlite3 db = sqlite3.connect(':memory:') c = db.cursor() c.execute('''CREATE TABLE users(id INTEGER PRIMARY KEY, name TEXT, phone TEXT)''') users = [ ('John', '5557241'), ('Adam', '5547874'), ('Jack', '5484522'), ('Monthy',' 6656565') ] c.executemany('''INSERT INTO users(name, phone) VALUES(?,?)''', users) db.commit() # Print the users c.execute('''SELECT * FROM users''') for row in c: print(row) db.close() |
Execute SQL File with SQLite's executescript
The executemethod only allows you to execute a single SQL sentence. If you need to execute several different
SQL sentences you should use
executescriptmethod:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | # Import the SQLite3 module import sqlite3 db = sqlite3.connect(':memory:') c = db.cursor() script = '''CREATE TABLE users(id INTEGER PRIMARY KEY, name TEXT, phone TEXT); CREATE TABLE accounts(id INTEGER PRIMARY KEY, description TEXT); INSERT INTO users(name, phone) VALUES ('John', '5557241'), ('Adam', '5547874'), ('Jack', '5484522');''' c.executescript(script) # Print the results c.execute('''SELECT * FROM users''') for row in c: print(row) db.close() |
1 2 3 4 | fd = open('myscript.sql', 'r') script = fd.read() c.executescript(script) fd.close() |
try/except/elseclause in order to catch
the exceptions. To learn more about the
try/except/elsekeywords, checkout the Catching
Python Exceptions – The try/except/else keywords article.
Defining SQLite SQL Functions
Sometimes we need to use our own functions in a statement, specially when we are inserting data in order to accomplish some specific task. A good example of this is when we are storing passwords in the database and we need to encrypt those passwords:1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | import sqlite3 #Import the SQLite3 module import hashlib def encrypt_password(password): # Do not use this algorithm in a real environment encrypted_pass = hashlib.sha1(password.encode('utf-8')).hexdigest() return encrypted_pass db = sqlite3.connect(':memory:') # Register the function db.create_function('encrypt', 1, encrypt_password) c = db.cursor() c.execute('''CREATE TABLE users(id INTEGER PRIMARY KEY, email TEXT, password TEXT)''') user = ('johndoe@example.com', '12345678') c.execute('''INSERT INTO users(email, password) VALUES (?,encrypt(?))''', user) |
create_functiontakes 3 parameters:
name(the
name used to call the function inside the statement), the number of parameters the function expects (1 parameter in this case) and a callable object (the function itself). To use our registered function, we called it using
encrypt()in
the statement.
Finally, PLEASE use a true encryption algorithm when you are storing passwords!
via:http://www.pythoncentral.io/advanced-sqlite-usage-in-python/
相关文章推荐
- Python SQLite3的问题sqlite3.ProgrammingError: SQLite objects created in a thread can only be used in th
- Pro Deep Learning with TensorFlow: A Mathematical Approach to Advanced Artificial Intelligence in Python
- Use Sqlite in Python
- 多线程下,Python Sqlite3报[SQLite objects created in a thread can only be used...]问题
- the usage of dict in python
- Introduction to SQLite in Python
- sqlite in python
- the usage of tuple in python
- Advanced usage in vim
- Python 入门教程 15 ---- Advanced Topics in Python
- spaCy is a library for advanced natural language processing in Python and Cython:spaCy 工业级自然语言处理工具
- Python Notes (6) - Advanced Topics in Python
- sqlite3(python3.4 demo in doc)
- python error when compile file in windows
- [LeetCode]题解(python):106-Construct Binary Tree from Inorder and Postorder Traversal
- Dive in Python学习笔记五:对象和面向对象
- Python基础--python操作sqlite
- [LeetCode]Search in Rotated Sorted Array@python
- [zz]How to produce html unit test output in Python?
- 二分K-均值算法 bisecting K-means in Python