最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Python3實(shí)現(xiàn)連接SQLite數(shù)據(jù)庫的方法

 更新時間:2014年08月23日 14:53:14   投稿:shichen2014  
這篇文章主要介紹了Python3實(shí)現(xiàn)連接SQLite數(shù)據(jù)庫的方法,在Python數(shù)據(jù)庫編程中有著廣泛的應(yīng)用,需要的朋友可以參考下

本文實(shí)例講述了Python3實(shí)現(xiàn)連接SQLite數(shù)據(jù)庫的方法,對于Python的學(xué)習(xí)有不錯的參考借鑒價值。分享給大家供大家參考之用。具體方法如下:

實(shí)例代碼如下:

import sqlite3

db = r"D:\pyWork\test.db"  #pyWork目錄下test.db數(shù)據(jù)庫文件
drp_tb_sql = "drop table if exists staff"
crt_tb_sql = """
create table if not exists staff(
  id integer primary key autoincrement unique not null,
  name varchar(100),
  city varchar(100)
);
"""

#連接數(shù)據(jù)庫
con = sqlite3.connect(db)
cur = con.cursor()

#創(chuàng)建表staff
cur.execute(drp_tb_sql)
cur.execute(crt_tb_sql)

#插入記錄
insert_sql = "insert into staff (name,city) values (?,?)"  #?為占位符
cur.execute(insert_sql,('Tom','New York'))
cur.execute(insert_sql,('Frank','Los Angeles'))
cur.execute(insert_sql,('Kate','Chicago'))
cur.execute(insert_sql,('Thomas','Houston'))
cur.execute(insert_sql,('Sam','Philadelphia'))

con.commit()

#查詢記錄
select_sql = "select * from staff"
cur.execute(select_sql)

#返回一個list,list中的對象類型為tuple(元組)
date_set = cur.fetchall()
for row in date_set:
  print(row)

cur.close()
con.close()

希望本文實(shí)例對大家的Python學(xué)習(xí)有所幫助。

相關(guān)文章

最新評論

双江| 巴林左旗| 凌海市| 万载县| 苏尼特右旗| 普兰店市| 平安县| 延吉市| 德江县| 邢台市| 天峻县| 霸州市| 嘉黎县| 当雄县| 呈贡县| 绵竹市| 广丰县| 连江县| 荆州市| 体育| 荣昌县| 浙江省| 乌苏市| 确山县| 石景山区| 白河县| 江川县| 洮南市| 商河县| 新建县| 天台县| 祁阳县| 桐柏县| 武冈市| 临湘市| 万源市| 常德市| 平江县| 鄄城县| 平和县| 镇平县|