OurSQL is the MySQL driver for Python, more here. I fail with the connection, I doubt the problem to be with port or host -- more details here about environment vars, I am working with Ubuntu.
$ 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')
asked May 17, 2012 at 0:15
hhh
53.3k64 gold badges188 silver badges297 bronze badges
1 Answer 1
You can't simply print the result of curs.execute(...) in this way. You're supposed to use the fetchone(...) or fetchmany(...) or fetchall(...) methods of the cursor object to retrieve its results.
Also, as the API documentation points out, iterating over the cursor is equivalent to repeatedly calling fetchone(). So, your script could end with something like:
curs.execute('SELECT * from test.pic')
for row in curs:
print(row)
answered May 17, 2012 at 0:19
srgerg
19.4k4 gold badges59 silver badges40 bronze badges
Sign up to request clarification or add additional context in comments.
1 Comment
hhh
Thank you, got it working -- nice
a=curs.fetchall(); print(a), +1.default