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等直接可以搞起。