-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
cursor.execute(); cursor.fetchall() returns empty tuple for first two calls on newly dropped/recreated/populated table #399
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
You haven't closed cursor used for create and insert into tweet table. |
Changed the code to only use one cursor. Now it's still returning an empty tuple for the first query after c = connection.cursor()
c.execute("""
select * from tweets
""")
print("query 1:")
print(c.fetchall())
c.execute("""
drop table if exists tweets;
create table tweets (
id int primary key auto_increment,
handle varchar(10),
text varchar(100)
);
insert into tweets (handle, text) values
('yolo123', 'whatup'),
('user11', 'wazzzap');
""")
print('result of drop/create/insert')
print(c.fetchall())
connection.commit()
c.execute("""
select 1
""")
print("query after commit:")
print(c.fetchall())
c.execute("""
select 1
""")
print("2nd query after commit:")
print(c.fetchall()) outputs: query 1:
[{'id': 1, 'handle': 'yolo123', 'text': 'whatup'}, {'id': 2, 'handle': 'user11', 'text': 'wazzzap'}]
result of drop/create/insert
()
query after commit:
() # should be [{'1':1}]
2nd query after commit:
[{'1': 1}] |
Interestingly, removing the statement import pymysql
import config
connection = pymysql.connect(
host= config.MYSQL_HOST,
user= config.MYSQL_USER,
password= config.MYSQL_PASSWORD,
db= config.TEST_DB_NAME,
charset= 'utf8mb4'
)
c = connection.cursor()
c.execute("""
select * from tweets
""")
print("query 1:")
print(c.fetchall())
c.execute("""
drop table if exists tweets;
create table tweets (
id int primary key auto_increment,
handle varchar(10),
text varchar(100)
);
insert into tweets (handle, text) values
('yolo123', 'whatup'),
('user11', 'wazzzap');
""")
print('result of drop/create/insert')
print(c.fetchall())
#connection.commit()
c.execute("""
select * from tweets
""")
print("query after commit:")
print(c.fetchall())
c.execute("""
select * from tweets
""")
print("2nd query after commit:")
print(c.fetchall())
|
I'm sorry. #403 was not perfect. It doesn't handle conn.commit() case.
This query has 4 statements. All of 4 statements don't returns rows.. receive queue: In this case, PyMySQL only read first "OK packet". receive queue: You should call MySQL also returns "OK packet" for commit. receive queue:
Since cursor think "has next", it fetches results until receive result not "has next" before send the query. receive queue: Then, cursor sends the query and MySQL returns "resultset" for it. receive queue: Then, cursor fetches "OK" and it think "this query has not resultset".
Since the result cursor having is not "has next", cursor sends second query and MySQL returns resultset for it. receive queue: Then cursor reads
This is what happens in your case. |
This may be similar to #390, I don't know enough about MySQL to be able to tell.
The output is as follows:
Interestingly, even if you add a
time.sleep(1)
at the end of the for loop, it still returns an empty tuple for the first twoc.execute(); c.fetchall()
cycles.If you edit the for loop to the following,
It prints out 5 empty tuples for the first two times c.execute is called.
Version info:
MySQL server version: 5.5.44-0ubuntu0.14.04.1
The text was updated successfully, but these errors were encountered: