Python3 connect Sqlite3

hxy    2018-06-15 15:26

Python3

SQLite3



import sqlite3

conn = sqlite3.connect("sqlite.db")  #创建sqlite.db数据库
print ("open database success")
conn.execute("drop table IF EXISTS student")
query = """create table IF NOT EXISTS student(
    customer VARCHAR(20),
    produce VARCHAR(40),
    amount FLOAT,
    date DATE   
);"""
conn.execute(query)
print ("Table created successfully")

#在表中插入数据

''' 方法1 '''
#data = '''INSERT INTO student(customer,produce,amount,date)\
#    VALUES("zhangsan","notepad",999,"2017-01-02")'''
#conn.execute(data)
#data = '''INSERT INTO student(customer,produce,amount,date)\
#    VALUES("lishi","binder",3.45,"2017-04-05")'''
#conn.execute(data)
#conn.commit()

''' 方法2 '''
statement = "INSERT INTO student VALUES(?,?,?,?)"
data = [("zhangsan","notepad",999,"2017-01-02"),("lishi","binder",3.45,"2017-04-05")]
conn.executemany(statement, data)
conn.commit()

curson = conn.execute("select * from student")
conn.commit()
print (curson)
rows = curson.fetchall()
print (rows)
conn.close()

 

Views: 1.5K

[[total]] comments

Post your comment
  1. [[item.time]]
    [[item.user.username]] [[item.floor]]Floor
  2. Click to load more...
  3. Post your comment