sqlite3 での Python 操作のコード例

Y2J
リリース: 2017-05-04 11:37:25
オリジナル
1222 人が閲覧しました

这篇文章主要介绍了Python简单操作sqlite3的方法,结合实例形式分析了Python针对sqlite3数据库的读取、创建、增删改查等基本操作技巧,需要的朋友可以参考下

import sqlite3
def Test1():
  #con =sqlite3.connect("D:\\test.db")
  con =sqlite3.connect(":memory:") #store in memory
  cur =con.cursor()
  try:
    cur.execute('create table score(id integer primary key,name varchar(10),scores integer)')
    cur.execute("insert into score values(0,'Rose',87)")
    cur.execute("insert into score values(1,'Alice',78)")
    cur.execute("insert into score values(2,'Helon',100)")
    cur.execute("insert into score values(3,'Tom',98)")
    cur.execute("insert into score values(4,'jack',198)")
    cur.execute("insert into score values(5,'Tony',198)")
    cur.execute("insert into score values(6,'David',99)")
    cur.execute("update score set scores =? where id=?",(45,3))
    cur.execute("update score set name=? where id=?",("John",0))
    cur.execute("delete from score where id =1")
  except Exception,e:
    print "There are some except",e
  con.commit()
  print "Insert Complete"
  print "-----------------------------------------"
  print "Last row id is ",cur.lastrowid
  cur.execute('select * from score')
  print cur.fetchall()
  print "----------------------------------------"
  cur.execute("select count(*) from score")
  print "Current Rows is :",cur.fetchall()[0]
  cur.close()
  con.close()
if name =='main':
  Test1()
  print "hello world"
ログイン後にコピー

以上がsqlite3 での Python 操作のコード例の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

関連ラベル:
ソース:php.cn
このウェブサイトの声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。
最新の問題
人気のチュートリアル
詳細>
最新のダウンロード
詳細>
ウェブエフェクト
公式サイト
サイト素材
フロントエンドテンプレート
私たちについて 免責事項 Sitemap
PHP中国語ウェブサイト:福祉オンライン PHP トレーニング,PHP 学習者の迅速な成長を支援します!