python for andorid : import sqlite3 测试代码

本文介绍了一个使用Python和SQLite3模块进行数据库查询的例子。通过用户输入的六位数代码,程序将查询三个不同级别的数据,并展示如何连接到数据库、执行SQL语句及获取查询结果。

test.db 放入 /sl4a/

test_sqlite.py 测试代码

# -*- coding: utf-8 -*-
import android
import sqlite3
import sys
reload(sys)
sys.setdefaultencoding('utf-8')

droid = android.Android()
code = droid.dialogGetInput('6位数code:').result
if len(code)==6:
    sh = code[0:2]
    city = code[0:4]
else:
    print 'input: code.length==6 '
    sys.exit(4)

db = sqlite3.connect("test.db")
cur = db.cursor()
cur.execute("select code,cname from table where code=? ",(sh+'0000',))
for t in cur.fetchone():
    print t,
print
cur.execute("select code,cname from table where code=? ",(city+'00',))
for t in cur.fetchone():
    print t,
print
cur.execute("select code,cname from table where code=? ",(code,))
for t in cur.fetchone():
    print t,
print
#
cur.close()
db.close()


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值