昊天SEO

python sqlite3的常规使用

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()
网站的维护离不开大家的支持鼓励,捐赠让我更有动力走的更远&& 也可以关注我的微信公众号,发布更多的干货
本文网址:https://www.168seo.cn/python/1668.html

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址