python数据库SQLite

5 篇文章 0 订阅
1 篇文章 0 订阅

http://www.runoob.com/sqlite/sqlite-data-types.html

python和SQLlite ,SQLite是内嵌数据库

import sqlite3
cx=sqlite3.connect("D:/database.db")
cur=cx.cursor()
#cur.execute('create table users(login varchar(8),uid integer)')
cur.execute('insert into users values("join",100)')
cur.execute('select * from users')
for eachuser in cur.fetchall():
    print eachuser

cur.close()
cx.commit()
cx.close()

数据库的几种数据类型

Date 时间格式  varchar(n) n字符格式 int 整型数据

try:
     cur.execute('insert into users values("join",100)')
except:
     cur.execute('create table users(login varchar(8),uid integer)')
用try来测试没有创建的表格

其实还会有其他错误,用error的属性来判断就可以了。其他更好的方法我也不知道 


搜索语句

select bname from business where bname like '%Pizza%'


加上排序

select bname,bcount from business where bname like '%Pizza%' order by bcount desc

选取前10个

select bname,bcount from business where bname like '%Pizza%' order by bcount desc limit 10


测试某个表是否存在

"select count(*) from sqlite_master where type='table' and name='SavedData'"
返回的数据格式,你要取[0][0]

两个表之间

select id, sentence from Reviews where id in (select id from SavedData where data = '1000')


cur.fetchall() 

分组统计

SELECT stars,count(stars) from reviews WHERE business_id = "eT5Ck7Gg1dBJobca9VFovw" GROUP BY stars



数据库出错,一个是语法问题,第二个问题是有时候数据本身有一些特殊字符,需要转义一下?? 比如双引号就不行,还有-也是不行的。

关于SQLite

官方网站:

http://www.sqlite.org/about.html 这个吧

中文的图书,有一本《SQLite权威指南》 好像已经买不到新书了


有一个可视化管理数据库的软件

http://www.cr173.com/soft/36343.html

上面那个数据量有点大的时候,后面死活打不开了


一个很简单的数据库管理软件,后来电脑下了几个dll

http://sqlitebrowser.org/


回Py的主要函数

http://blog.csdn.net/artemisrj/article/details/39254741

select id, sentence from Reviews where id in (select id from SavedData where data = '1000')

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值