mirror of
https://github.com/rasbt/python_reference.git
synced 2024-11-23 20:11:13 +00:00
sqlite3
This commit is contained in:
parent
5cdf9fd0ea
commit
49a0f48110
4
sqlite3_examples/README.md
Normal file
4
sqlite3_examples/README.md
Normal file
|
@ -0,0 +1,4 @@
|
|||
sqlite3_examples
|
||||
================
|
||||
|
||||
Syntax examples for working with SQLite databases via the sqlite3 module in Python
|
30
sqlite3_examples/create_db.py
Normal file
30
sqlite3_examples/create_db.py
Normal file
|
@ -0,0 +1,30 @@
|
|||
# 10/28/2013 Sebastian Raschka
|
||||
# Syntax basics for creating sqlite3 data bases
|
||||
|
||||
import sqlite3
|
||||
|
||||
# create new db and make connection
|
||||
conn = sqlite3.connect('zinc_db1.db')
|
||||
c = conn.cursor()
|
||||
|
||||
# create table
|
||||
c.execute('''CREATE TABLE zinc_db1
|
||||
(zinc_id TEXT, purchasable TEXT, non_rot_bonds INT)''')
|
||||
|
||||
# Insert one row of data
|
||||
c.execute("INSERT INTO zinc_db1 VALUES ('ZINC00895032','YES', 4)")
|
||||
|
||||
# Insert multiple lines of data
|
||||
multi_lines =[ ('ZINC00895033','YES', 1),
|
||||
('ZINC00895034','NO', 0),
|
||||
('ZINC00895035','YES', 3),
|
||||
('ZINC00895036','YES', 9),
|
||||
('ZINC00895037','YES', 10)
|
||||
]
|
||||
c.executemany('INSERT INTO zinc_db1 VALUES (?,?,?)', multi_lines)
|
||||
|
||||
# Save (commit) the changes
|
||||
conn.commit()
|
||||
|
||||
# close connection
|
||||
conn.close()
|
28
sqlite3_examples/query_db.py
Normal file
28
sqlite3_examples/query_db.py
Normal file
|
@ -0,0 +1,28 @@
|
|||
# 10/28/2013 Sebastian Raschka
|
||||
# Syntax basics for querying sqlite3 data bases
|
||||
|
||||
import sqlite3
|
||||
|
||||
# open existing database
|
||||
conn = sqlite3.connect('zinc_db1.db')
|
||||
c = conn.cursor()
|
||||
|
||||
# print all lines ordered by number of non_rot_bonds
|
||||
for row in c.execute('SELECT * FROM zinc_db1 ORDER BY non_rot_bonds'):
|
||||
print row
|
||||
|
||||
# print all lines that are purchasable and have <= 7 rotatable bonds
|
||||
t = ('YES',7,)
|
||||
for row in c.execute('SELECT * FROM zinc_db1 WHERE purchasable=? AND non_rot_bonds <= ?', t):
|
||||
print row
|
||||
|
||||
# print all lines that are purchasable and have <= 7 rotatable bonds
|
||||
t = ('YES',7,)
|
||||
c.execute('SELECT * FROM zinc_db1 WHERE purchasable=? AND non_rot_bonds <= ?', t)
|
||||
rows = c.fetchall()
|
||||
for r in rows:
|
||||
print r
|
||||
|
||||
# close connection
|
||||
conn.close()
|
||||
|
34
sqlite3_examples/update_db.py
Normal file
34
sqlite3_examples/update_db.py
Normal file
|
@ -0,0 +1,34 @@
|
|||
# 10/28/2013 Sebastian Raschka
|
||||
# Syntax basics for updating sqlite3 data bases
|
||||
|
||||
import sqlite3
|
||||
|
||||
# make connection to existing db
|
||||
conn = sqlite3.connect('zinc_db1.db')
|
||||
c = conn.cursor()
|
||||
|
||||
# update field
|
||||
t = ('NO', 'ZINC00895033', )
|
||||
c.execute("UPDATE zinc_db1 SET purchasable=? WHERE zinc_id=?", t)
|
||||
print "Total number of rows changed:", conn.total_changes
|
||||
|
||||
# delete rows
|
||||
t = ('NO', )
|
||||
c.execute("DELETE FROM zinc_db1 WHERE purchasable=?", t)
|
||||
print "Total number of rows deleted: ", conn.total_changes
|
||||
|
||||
# add column
|
||||
c.execute("ALTER TABLE zinc_db1 ADD COLUMN 'keto_oxy' TEXT")
|
||||
|
||||
# save changes
|
||||
conn.commit()
|
||||
|
||||
# print column names
|
||||
c.execute("SELECT * FROM zinc_db1")
|
||||
col_name_list = [tup[0] for tup in c.description]
|
||||
print col_name_list
|
||||
|
||||
|
||||
|
||||
# close connection
|
||||
conn.close()
|
Loading…
Reference in New Issue
Block a user