3

【丢弃】【Python】sqlite

 3 years ago
source link: https://www.guofei.site/2017/04/26/pysqlite.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

【丢弃】【Python】sqlite

2017年04月26日

Author: Guofei

文章归类: ,文章编号:


版权声明:本文作者是郭飞。转载随意,但需要标明原文链接,并通知本人
原文链接:https://www.guofei.site/2017/04/26/pysqlite.html

Edit

回收原因:sqlalchemy对sqlite3和pymysql进行了良好的封装,因此在操作sql时,无需再过多关注底层的sqlite3和pymysql了

创建

cx = sqlite3.connect("E:/test.db")
cx= sqlite3.connect(':memory:') #创建在内存上

存在则打开,不存在则创建

操作

  • commit()–事务提交
  • rollback()–事务回滚
  • close()–关闭一个数据库连接
  • cursor()–创建一个游标

游标

cu = cx.cursor()

这样定义了一个游标。游标对象有以下的操作:

  • execute()–执行sql语句
  • executemany–执行多条sql语句
  • close()–关闭游标
  • fetchone()–从结果中取一条记录,并将游标指向下一条记录
  • fetchmany()–从结果中取多条记录
  • fetchall()–从结果中取出所有记录
  • scroll()–游标滚动

建表

cu.execute('create table catalog (id integer primary key,pid integer,name varchar(10) UNIQUE)')

插入数据

cu.execute("insert into catalog values(0, 0, 'name1')")
cu.execute("insert into catalog values(1, 0, 'hello')")
cx.commit()

查询数据

cu.execute('select * from catalog')
cu.fetchall()

修改数据

cu.execute("update catalog set name='name2' where id = 0")
cx.commit()

收尾

cu.close()
cx.close()

您的支持将鼓励我继续创作!

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK