I'm execute a simple mssql query from python.
I can see in the profiler that the query reach the DB.
The query has 1 row of answer.
I fail to see the output in the Python shell
I run the code below
import pymssql
conn = pymssql.connect(host='SQL01', user='user', password='password', database='mydatabase', as_dict=True)
cur = conn.cursor()
cur.execute('SELECT * FROM persons WHERE salesrep=%s', 'John Doe')
for row in cur:
print "ID=%d, Name=%s" % (row['id'], row['name'])
Pleas advise
Thanks,
Assaf
You can call fetchone() or fetchall() after execute to get the data from that query.
import pymssql
conn = pymssql.connect(host='SQL01', user='user', password='password', database='mydatabase', as_dict=True)
cur = conn.cursor()
cur.execute('SELECT * FROM persons WHERE salesrep=%s', 'John Doe')
print cur.fetchall()
import pymssql
conn = pymssql.connect(host='SQL01', user='user', password='password', database='mydatabase', as_dict=True)
cur = conn.cursor()
users = cur.execute('SELECT * FROM persons WHERE salesrep=%s', 'John Doe').fetchall()
conn.close()
for row in users:
print "ID=%d, Name=%s" % (row['id'], row['name'])
Try assigning the results to something instead of using the cursor.
cur.execute() is a function, as such while it does return a value (which you saw), you're not assigning it to anything, so when you go to do the for loop, there's nothing to loop over.
If you don't want to store the result, you could do this (rather messy) version:
import pymssql
conn = pymssql.connect(host='SQL01', user='user', password='password', database='mydatabase', as_dict=True)
cur = conn.cursor()
sql = 'SELECT * FROM persons WHERE salesrep=%s'
for row in cur.execute(sql, 'John Doe').fetchall():
print "ID=%d, Name=%s" % (row['id'], row['name'])
conn.close()
This one does the for loop over the result of the cur.execute(), but I really advise against this
(Minor addendum: I forgot the fetchall's, I'm so used to putting this in a function. Sorry)
Related
I am trying to query MS SQL Server for a table.column, then insert this output into a sqlite table.
This example has one numeric column in the SQL Server source table.
I think I've almost got it by scouring the other answers.
Please let me know what I am missing.
import sqlite3
import pyodbc
#def connect_msss():
ODBC_Prod = ODBC_Prod
SQLSN = SQLSN
SQLpass = SQLpass
conn_str = ('DSN='+ODBC_Prod+';UID='+SQLSN+';PWD='+SQLpass)
conn = pyodbc.connect(conn_str)
#def connect_sqlite():
sl3Conn = sqlite3.connect('server_test.db')
c = sl3Conn.cursor()
c.execute('CREATE TABLE IF NOT EXISTS mrn_test (PTMRN NUMERIC)')
#def query_msss():
cur = conn.cursor()
cur.execute("SELECT TOP 50 PTMRN FROM dbo.atl_1234_mrntest")
rows = cur.fetchall()
for row in rows:
c.execute("INSERT INTO mrn_test VALUES (?)", row)
conn.commit()
#connect_msss()
#connect_sqlite()
#query_msss()
Error 1:
c.execute('CREATE TABLE IF NOT EXISTS mrn_test
(PTMRN NUMERIC)')
Out[117]: <sqlite3.Cursor at 0x2d1a742fc70>
Error 2:
cur = conn.cursor() cur.execute("SELECT TOP 50 PTMRN FROM
dbo.atl_1234_mrntest")
Out[118]: <pyodbc.Cursor at 0x2d1a731b990>
You're not committing the executed changes on the sqlite connection, after the c.execute step you're committing the MySQL DB connection. I think you need to replace conn.commit() at the end with sl3Conn.commit().
For example PyMySQL. How will cursors be used more correctly?
Option 1:
connect = pymysql.connect()
cursor = connect.cursor()
cursor.execute('INSERT INTO user(name) VALUE('%s')', ('John',))
last_id = cursor.lastrowid
cursor.execute('SELECT name FROM user')
result = cursor.fetchall()
cursor.close()
Option 2:
connect = pymysql.connect()
cursor = connect.cursor()
cursor.execute('INSERT INTO user(name) VALUE('%s')', ('John',))
last_id = cursor.lastrowid
cursor.close()
cursor = connect.cursor()
cursor.execute('SELECT name FROM user')
result = cursor.fetchall()
cursor.close()
Those. Can I use a single cursor for all queries, or create a new cursor each time for a new query?
And, if you use one cursor for one type of query with different data? For example, option 3:
connect = pymysql.connect()
cursor = connect.cursor()
cursor.execute('INSERT INTO user(name) VALUE('%s')', ('John',))
last_id_1 = cursor.lastrowid
cursor.execute('INSERT INTO city(name) VALUE('%s')', ('Moskow',))
last_id_2 = cursor.lastrowid
cursor.close()
cursor = connect.cursor()
cursor.execute('SELECT name FROM user')
result_1 = cursor.fetchall()
cursor.execute('SELECT name FROM city')
result_2 = cursor.fetchall()
cursor.close()
Which option is better/more correct to use (and in terms of performance too)?
You don't need to Repeat it for Each Time. You Only need it one time..
// Creating the Connection
import pymysql.cursors
connection = pymysql.connect(host='localhost',
user='root',
password='pwd',
db='db',
charset='utf8mb4',
cursorclass=pymysql.cursors.DictCursor)
cursor = connection.cursor()
cursor.execute('INSERT INTO user(name) VALUE('%s')', ('John',))
last_id_1 = cursor.lastrowid
cursor.execute('INSERT INTO city(name) VALUE('%s')', ('Moskow',))
last_id_2 = cursor.lastrowid
cursor.execute('SELECT name FROM user')
result_1 = cursor.fetchall()
cursor.execute('SELECT name FROM city')
result_2 = cursor.fetchall()
cursor.close()
To apply the changes to DB for Insert or Update Query you should add the below code snippet also.
cursor.commit()
i have python script to get domains from sql to nginx.
#!/usr/bin/python3
import MySQLdb
query = 'SELECT name_of_domain FROM domain_table'
database_connect = MySQLdb.connect(host=host, user=user, passwd=password, db=database, port=port)
cursor = database_connect.cursor()
cursor.execute(query)
while True:
row = cursor.fetchone()
print (row)
In this case everything is ok. In a loop, i have received line by line all rows.
I decided to use function:
My function:
def get_cursor():
database_connect = MySQLdb.connect(host=host, user=user, passwd=password, port=port, db=database, charset='utf8')
database_connect.autocommit(True)
return database_connect.cursor(MySQLdb.cursors.DictCursor)
And i have tried to use this:
#!/usr/bin/python3
import MySQLdb
cursor = get_cursor()
query = 'SELECT name_of_domain FROM domain_table'
cursor.execute(query)
while True:
row = cursor.fetchone()
print (row)
But in this case, i have received only one result, and my next functions don't work. Where i have error ? Please help.
I'm not 100% sure, but think that this is because the cursor.execute() returns an iterator, which is used up by the first .fetchone() call.
see https://dev.mysql.com/doc/connector-python/en/connector-python-api-mysqlcursor-fetchone.html
try replacing
while True:
row = cursor.fetchone()
print (row)
with
for row in cursor:
print(row)
I'm a bit curious what is the pythonic/best way to solve my issue.
A short code example:
import pymssql
conn = pymssql.connect("SERVER", 'sa', 'PASSWORD', 'DATABASE', charset='utf8')
cursor = conn.cursor()
sql = "SELECT 'foo\bar' as bs_field"
cursor.execute(sql)
row = cursor.fetchone()
print row[0]
# missing \, returns u'foobar'
sql = "select FIELD_CONTAINING_BACKSLASH from TABLE"
cursor.execute(sql)
row = cursor.fetchone()
print row[0]
# all OK here
sql = "SELECT 'foo\\bar' as bs_field"
cursor.execute(sql)
row = cursor.fetchone()
print row[0]
# this is OK too
I want to know why the \ is missing in the first example - is there a better solution as quoting every single sql?
I am an idiot!
Has nothing to do with mssql, it's just python strings.
r'bla\bla'
'bla\\bla'
Reference: https://docs.python.org/2.0/ref/strings.html
im dealing with strage problem and this is like this:
this query should return all of my table:
db = MySQLdb.connect(host="localhost", port=3306, user="A", passwd="B", db="X")
cursor = db.cursor()
cursor.execute("select * from mytable")
cursor.fetchall()
for row in cursor:
print row
for loop should print all rows in cursor but it will only print the first one.
it seems cursor is filled with first row only.
is there anything that i missed here?
thanks
You need to put the output of cursor.fetchall() into a variable. Like
db = MySQLdb.connect(host="localhost", port=3306, user="A", passwd="B", db="X")
cursor = db.cursor()
cursor.execute("select * from mytable")
rows = cursor.fetchall()
for row in rows:
print row
You can try limit:
cursor.execute("select * from mytable limit 1")
Try
db = MySQLdb.connect(host="localhost", port=3306, user="A", passwd="B", db="X")
cursor = db.cursor()
for row in cursor.execute("select * from mytable"):
print row
you need a dic and save the result here
dic={}
cursor.execute("select * from table")
dic['table']=cursor.fetchall()
for row in range(len(dic['table'])):
print dic['table'][row]
and if you need print any colum
print dic['table'][row]['colum']
This is not the correct way to use the .fetchall() method. Use cursor.stored_results() and then do a fetchall() on the results to perform this task, like this:
db = MySQLdb.connect(host="localhost", port=3306, user="A", passwd="B", db="X")
cursor = db.cursor()
cursor.execute("select * from mytable")
results = cursor.stored_results()
for result in results:
print result.fetchall()
I also had this problem. My mistake was that after inserting new row in the table I didn't commit the result. So you should add db.commit() after INSERT command.
i know its been very long time since, but i didnt find this answer anywhere else and thought it might help.
cursor.execute("SELECT top 1 * FROM my_table")