python操作sqlite3的示例

傻無盡 9年前發布 | 7K 次閱讀 Python SQLite

python sqlite3的常規使用    

#Filename: DBcon.py

import sqlite3
import time

conn=sqlite3.connect(r'D:\Exercise\Python\DB\example.db')
c=conn.cursor()
c.execute("Create table if not exists stocks (date text, trans text, symbol text, qty real, price real)")
flag=raw_input("Do you wanna clean the table?(y/n):")
if flag=='y':
    c.execute("delete from stocks")
c.execute("Insert into stocks values('"+time.strftime('%Y-%m-%d %H:%m:%S')+"', 'Buy','Rhatd',1000,23.14)")
conn.commit()
c.execute('select * from stocks')
res=c.fetchall()
count=0
print res
print '-'*60
for line in res:
    for f in line:
        count=count+1
        print count,f,
    print
print '-'*60
print '-'*60
print 'There are %d records!'  %count
c.close()
conn.close()
 本文由用戶 傻無盡 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!