python中sqlite
1. python sqlite3 如何模糊查询变量
刚刚研究了一下,我的代码是在python 3.2.3下的。不知你的版本是多少,姑且参考吧。 以下代码根据python的手册里的例子改编。import sqlite3 con = sqlite3.connect(":memory:") cur = con.cursor() cur.execute("create table people (name, age)") who = "Yeltsin"age = 72# This is the qmark style: cur.execute("insert into people values (?, ?)", (who, age)) # Query using like clause cur.execute("select * from people where name like ?",('%Yel%',))。
2. python中的sqlite3 把一个链表当做一个元素插入到数据库_表中一行记录中的一个属性中
>>>importsqlite3
>>>
>>>conn=sqlite3.connect(":memory:")
>>>conn.executescript("""
...createtabletester(idintegerprimarykey,lst);
...""")
<sqlite3.Cursorobjectat0xb7447460>
>>>conn.commit()
>>>curr=conn.cursor()
>>>curr.executemany("insertintotester(id,lst)values(?,?)",[
...(1,repr(range(10))),
...(2,repr(range(20))),
...(3,repr(range(30))),
...])
<sqlite3.Cursorobjectat0xb7447be0>
>>>conn.commit()
>>>curr.execute("select*fromtester")
<sqlite3.Cursorobjectat0xb7447be0>
>>>forid,lstincurr:
...printid,eval(lst)
...
1[0,1,2,3,4,5,6,7,8,9]
2[0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19]
3[0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29]
>>>
3. 如何在python程序中查看sqlite3某数据库中的表名
sqlite3数据库里表的信息存储在了一个名为sqlite_master的表中
因此可以通过这条语句回来查看数据库中答所有表的名称
SELECT name FROM sqlite_master WHERE type='table';
下面是Python的用法
con=sqlite3.connect('database.db')
cursor=con.cursor()
cursor.execute("SELECTnameFROMsqlite_masterWHEREtype='table';")
print(cursor.fetchall())
4. python sqlite中的问题
因为你要插入的是字符串,但是按照你现在这样的写法,两边就少了引号,字符串要加引号才行。
5. Python和SQLite问题,怎么解决
python编码如果把中文数据存储至sqlite数据库某一字段中,再通过查询语句取出并进行相关的字符串操作时,经常会出现错误提示,类似于UnicodeDecodeError,提示某一类型编码不能转换。
出现这个问题的原因是因为python默认使用unicode处理sqlite3的TEXT类型(varchar类型也是如此,因为在sqlite中varchar其实就是TEXT)。python把中文存入数据库时使用了类似于GBK这样的编码,取出时会尝试把TEXT类型数据转换成unicode,从而出现错误。
由此导致的另一个不容易发现的错误是存储在数据库中的中文进行了base64之类的编码,在python中取出时不会存在错误,但是再进行base64解码,并与sqlite3中取出的其它text字段进行字符串拼接等处理,就出现编码转换错误,很难发现问题原因,可以把其它text字段进行如'aaa'.encode('gbk')编码成GBK码解决,但不提倡这种方法,更好方法如下:
解决方法是python连接sqlite数据库后进行如下设置:
conn = sqlite3.connection(“……”)
conn.text_factory = str
另外为了python代码中硬编码的中文字符串不出现问题,除了在源码开始添加
# -*- coding:utf-8 -*-
还要设置python源码的编码为utf-8
import sys
reload(sys)
sys.setdefaultencode('utf8')
6. python中使用SQlite3问题
importsqlite3
conn=sqltie3.connect(database='dbfile.db')
curr=conn.cursor()
defgetstudent(studentid):
curr=conn.cursor()
curr.execute("SELECT*FROMtb_studentWHEREid=?",(studentid,))
returncurr.fetchall()
...
7. 如何打开python自带sqlite shell
sqlite3数据库里表的信息存储在了一个名为sqlite_master的表中
因此可以通过这条语句来查看数据库中所回有表的名称
SELECT name FROM sqlite_master WHERE type='table';
下面是Python的用法答
con = sqlite3.connect('database.db')
cursor = con.cursor()
cursor.execute("SELECT name FROM sqlite_master WHERE type='table';")
print(cursor.fetchall())
8. Python中SQLite支持数据库远程访问吗
使用自己的文件锁解决这个问题。
Multiple processes can have the same database open at the same time. Multiple processes can be doing a SELECT at the same time. But only one process can be making changes to the database at any moment in time, however.
SQLite uses reader/writer locks to control access to the database. (Under Win95/98/ME which lacks support for reader/writer locks, a probabilistic simulation is used instead.) But use caution: this locking mechanism might not work correctly if the database file is kept on an NFS filesystem. This is because fcntl() file locking is broken on many NFS implementations. You should avoid putting SQLite database files on NFS if multiple processes might try to access the file at the same time. On Windows, Microsoft's documentation says that locking may not work under FAT filesystems if you are not running the Share.exe daemon. People who have a lot of experience with Windows tell me that file locking of network files is very buggy and is not dependable. If what they say is true, sharing an SQLite database between two or more Windows machines might cause unexpected problems.
We are aware of no other embedded SQL database engine that supports as much concurrency as SQLite. SQLite allows multiple processes to have the database file open at once, and for multiple processes to read the database at once. When any process wants to write, it must lock the entire database file for the ration of its update. But that normally only takes a few milliseconds. Other processes just wait on the writer to finish then continue about their business. Other embedded SQL database engines typically only allow a single process to connect to the database at once.
However, client/server database engines (such as PostgreSQL, MySQL, or Oracle) usually support a higher level of concurrency and allow multiple processes to be writing to the same database at the same time. This is possible in a client/server database because there is always a single well-controlled server process available to coordinate access. If your application has a need for a lot of concurrency, then you should consider using a client/server database. But experience suggests that most applications need much less concurrency than their designers imagine.
When SQLite tries to access a file that is locked by another process, the default behavior is to return SQLITE_BUSY. You can adjust this behavior from C code using the sqlite3_busy_handler() or sqlite3_busy_timeout() API functions.
qlite应该是只是一个本地文件,API放在各个语言的开发包里了,它本身不具备C/S的网络功能。
见官方文档:
“ If you have many client programs accessing a common database over a network, you should consider using a client/server database engine instead of SQLite.”
如果一定想支持远程访问有这么几条出路:
1、换其他支持网络访问的数据库如MySQL。
如果坚持要用Sqlite
2、楼上所述,用网络文件系统,但是不建议。因为随机读写在NFS等系统上的性能都很成问题,而且稳定性堪忧。
3、用RPC等封装一下,如Thrift、XML-RPC等,Java的话还有RMI等直接可以搞起。