This is the reproduced sample:
import mysql.connector
conn = mysql.connector.connect(
user='root', password='12347',
host='localhost')
def getCursor():
return conn.cursor()
def execQuery(cursor=getCursor()):
cursor.execute("SELECT 2")
cursor.fetchall()
cursor.close()
for i in range(4):
cursor = execQuery()
This code works without cursor.close(). But what I find weird is that this sample works even with cursor.close() with a simple change:
def execQuery():
cursor=getCursor()
cursor.execute("SELECT 2")
cursor.fetchall()
cursor.close()
By moving the default parameter to the body of the function.
I don't know if it's the best practice to close the cursor, so I can skip closing cursor while keeping the first form. If it's not the best practice to have a default parameter that uses return value of a function, I can go with the second form. But I want to why they act differently
It's like I'm having the same error as the following:
cursor.execute("SELECT 2")
cursor.fetchall()
cursor.close()
cursor.execute("SELECT 2")
It's like every call of execQuery is using the same cursor, so it gets blocked right at the second call.
When you need to connect to Database you need something like cursor. You need a cursor object to fetch results.
In the sample program when you run loop in range (4) it calls execQuery() . Looking into definition you can find def execQuery(cursor=getCursor()): the function takes input as cursor and by default it used getCursor() function which creates cursor everything when the loop is executed.
While in your program you are closing the cursor but not creating it again hence when second execute query comes there is no cursor present and the program throws an error.
Related
In a database program what does these lines of code mean and do?
conn=sqlite3.connect(filename)
c=conn.cursor()
conn.commit()
You could think of conn = sqlite3.connect(filename) as creating a connection, or a reference, to that database specified in the filename. So anytime you carry out an action with conn, it will be an action performed on the database specified by filename.
c = conn.cursor() is a cursor object, which allows you to carry out SQL queries on the database. It is created using a call on the conn variable created earlier, and so is a cursor object for that specific database. This is most commonly useful for its .execute() method, which is used to execute SQL commands on the database.
conn.commit() 'commits' the changes to the database; that is, when this command is called, any changes that had been made by the cursor will be saved to the database.
I'm attempting to run some MySQL queries and output the results in my Python program. I've created this function that is called and the cursor is passed through. However, I am running into a problem where running the below code will always return None / nothing.
Here is what I have:
def showInformation(cursor):
number_rows = 'SELECT COUNT(*) FROM information_schema.tables WHERE table_schema = "DB"'
testing = cursor.execute(number_rows)
print(testing)
When using the cursor object itself, I do not run into any problems:
for row in cursor:
print(row)
I guess you need:
print(cursor.fetchone())
because you are returning only a count and so you expect one row.
Calling execute is not supposed to return anything unless multi=True is specified according to mysql documentation. The programmer can only iterate the cursor like you did, or call fetchone to retrieve one row or call fetchall to retrieve all rows or call fetchmany to retrieve some rows.
def readswitch(x,y,connn,read):
x='create vlan'
y='global'
conn = sqlite3.connect('server.db')
if conn:
cur = conn.cursor()
run= cur.execute("SELECT command FROM switch WHERE function =? or type = ? ORDER BY key ASC",(x,y))
read = cur.fetchall()
return run;
for row in read:
print (readswitch())
I am going to search x and y in my database and I want it to return my sql statement for the command
but it seems cant run this function like
for row in read:
NameError: name 'read' is not defined
can anyone fix this error?
Your code has several problems, including argument passing and variable scope. I'm not sure what it's really trying to do. I suggest rewriting it with no function, just straight sequential execution. Once you get that working, try to pull out the function call.
I'm trying to get a cursor from stored procedure in txpostgres.
Psycopg2 has a named cursors which are working fine for it. But there is no curs = conn.cursor('name') statement in txpostgres.
Is there another way to get it ?
txpostgres doesn't have a named cursor feature. However, psycopg2's named cursors are just a convenience wrapper for PostgreSQL's cursors. I don't have much experience with stored procedures, but here's an example with a simple query:
#inlineCallbacks
def transaction(cursor):
yield cursor.execute('mycursor CURSOR FOR SELECT bigtable')
yield cursor.execute('FETCH ALL FROM mycursor')
data = yield cursor.fetchall()
conn.runInteraction(transaction)
I'm writing a python CGI script that will query a MySQL database. I'm using the MySQLdb module. Since the database will be queryed repeatedly, I wrote this function....
def getDatabaseResult(sqlQuery,connectioninfohere):
# connect to the database
vDatabase = MySQLdb.connect(connectioninfohere)
# create a cursor, execute and SQL statement and get the result as a tuple
cursor = vDatabase.cursor()
try:
cursor.execute(sqlQuery)
except:
cursor.close()
return None
result = cursor.fetchall()
cursor.close()
return result
My question is... Is this the best practice? Of should I reuse my cursor within my functions? For example. Which is better...
def callsANewCursorAndConnectionEachTime():
result1 = getDatabaseResult(someQuery1)
result2 = getDatabaseResult(someQuery2)
result3 = getDatabaseResult(someQuery3)
result4 = getDatabaseResult(someQuery4)
or do away with the getDatabaseeResult function all together and do something like..
def reusesTheSameCursor():
vDatabase = MySQLdb.connect(connectionInfohere)
cursor = vDatabase.cursor()
cursor.execute(someQuery1)
result1 = cursor.fetchall()
cursor.execute(someQuery2)
result2 = cursor.fetchall()
cursor.execute(someQuery3)
result3 = cursor.fetchall()
cursor.execute(someQuery4)
result4 = cursor.fetchall()
The MySQLdb developer recommends building an application specific API that does the DB access stuff for you so that you don't have to worry about the mysql query strings in the application code. It'll make the code a bit more extendable (link).
As for the cursors my understanding is that the best thing is to create a cursor per operation/transaction. So some check value -> update value -> read value type of transaction could use the same cursor, but for the next one you would create a new one. This is again pointing to the direction of building an internal API for the db access instead of having a generic executeSql method.
Also remember to close your cursors, and commit changes to the connection after the queries are done.
Your getDatabaseResult function doesn't need to have a connect for every separate query though. You can share the connection between the queries as long as you act responsible with the cursors.