2012-05-17 35 views
0

OurSQL是Python的MySQL驅動程序,更多here。我與連接失敗,我懷疑與端口或主機有關的問題 - 有關環境變量的更多詳細信息here,我正在使用Ubuntu。DB與MySQL驅動程序/ API的連接OurSQL for Python?

$ cat t.py 
import oursql 
conn=oursql.connect(db='test', user='root', passwd='hello') 
#, port=3306) 
#, host='127.0.0.1') 
conn=oursql.connect(db='test') 
curs = conn.cursor(oursql.DictCursor) 
curs = conn.cursor(try_plain_query=False) 
a=curs.execute('SELECT * from test.pic') 

print(a) 
$ cat test.sql 
select * from test.pic; 

$ python t.py |wc 
     1  1  5  
$ mysql test < test.sql |wc 
     9  78  610 

WHY DIFFERENT LENGTHS?? 

THIS LINE WRONG (above)???? 
conn=oursql.connect(db='test', user='root', passwd='hello') 

回答

3

您不能簡單地以這種方式打印curs.execute(...)的結果。您應該使用遊標對象的fetchone(...)fetchmany(...)fetchall(...)方法來檢索其結果。

此外,如API documentation指出的那樣,遍歷光標相當於重複調用fetchone()。所以,你的腳本可能會以類似的結尾:

curs.execute('SELECT * from test.pic') 

for row in curs: 
    print(row) 
+0

謝謝,得到它的工作 - 很好'a = curs.fetchall();打印(a)',+1。 – hhh