天天看點

python資料庫sqlite3_Python資料庫sqlite3的基本應用

Python資料庫sqlite3的基本應用。做資料方面的程式員對今天代碼中使用的庫是比較了解的。Python自帶的這個輕量級關系型資料庫SQLite的基本應用。SQLite的作用,作為後端資料庫,可以搭配Python建網站。SQLite的應用還不僅止于這些,它在其它領域也有廣泛的應用。像是在HTML5和移動端等。

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()

#www.iplaypy.com

玩蛇網文章,轉載請注明出處和文章網址:https://www.iplaypy.com/code/base/b2330.html

相關文章 Recommend