Python数据库sqlite3的基本应用,python数据库sqlite3,Python数据库sql


Python数据库sqlite3的基本应用。做数据方面的程序员对今天代码中使用的库是比较了解的。Python自带的这个轻量级关系型数据库SQLite的基本应用。SQLite的作用,作为后端数据库,可以搭配Python建网站。SQLite的应用还不仅止于这些,它在其它领域也有广泛的应用。像是在HTML5和移动端等。

import sqlite3import timeconn=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=0print resprint '-'*60for line in res:for f in line:count=count+1print count,f,printprint '-'*60print '-'*60print 'There are %d records!'  %countc.close()conn.close()#www.iplaypy.com

编橙之家文章,

评论关闭