import psycopg2
from contextlib import closing
import inspect
db = psycopg2.connect(<credentials>)
query = "SELCT * FROM \
<tablename> \
LIMIT 100;"
with closing(db.cursor()) as cur:
cur.execute(query)
results = cur.fetchall()
print(len(results))
db.close()
With the above simple query and code, I am trying to read results from a SELECT query but the results object always have a single record instead of all. Query is working fine in pgadmin query tool, how do I fix this? Thanks in advance for any suggestions here.
Edit #1 (for snakecharmerb): While checking / printing results, I am always getting a single row correctly giving the query results.
Edit #2 (for Expurple): Thanks for the suggestion, will try that. I believe that is not affecting the overall query result.
Related
Now, I have a study about python sqlite3 database. I think it is very simple problem but not allow next step. Could help me?
There is print OK on vscode terminal, but not revised to DB file. I'm searching several times but I can not fix it.
If I execute the code, it not sorting on DB files.
import sqlite3
conn = sqlite3.connect('sqliteDB1.db')
cursor = conn.cursor()
cursor.execute("SELECT * FROM member")
temp123 = cursor. fetchall()
print(temp123)
cursor.execute("SELECT * FROM member ORDER BY -code")
temp321 = cursor.fetchall()
conn.commit
print(temp321)
conn.close()
A select statement just returns data from a database, it will not modify it. Moreover, tables in SQL databases are inherently unordered sets. They have no intrinsic value, and you should never rely on the order of the rows that happens to be returned unless you explicitly sort it with an order by clause.
I am trying to find the latest entry in a MySQL database by using a query with SELECT MAX(id). I already get the latest id, so I know the query works, but now I want to use it in a while loop so I keep getting the latest entry with each iteration.
This is what I have so far:
import pymysql
con = pymysql.connect(host='.....', user='.....',
password='.....', database='.....')
cur = con.cursor()
while True:
query = "SELECT MAX(id) FROM reports"
cur.execute(query)
data = cur.fetchall()
last = (data[0][0])
print(last)
The problem is that I keep getting the same result after updating the database. For instance, right now I have 45 entries, so my script prints '45' in a while loop. But after I add another row to the table it keeps printing '45' instead of the '46' I would expect. When I stop the script and run it again, it will print '46' and keep printing this even after I add another row.
I have only started working with MySQL about two weeks ago, so I don't have all that much knowledge about it. I feel like I'm missing something really small here. What should I do? Any help would be greatly appreciated.
I had this same problem, and just wanted to make it clear for anyone else searching for the solution.
Setting autocommit to True solved my issue and didn't require calling a commit after each query.
import pymysql
con = pymysql.connect(host='.....', user='.....',
password='.....', database='.....')
con.autocommit = True
cur = con.cursor()
while True:
query = "SELECT MAX(id) FROM reports"
cur.execute(query)
data = cur.fetchall()
last = (data[0][0])
print(last)
Here is a link to the documentation
I am trying to run some querys that needs to create some temporary tables and then returns a result set, but i am unable to do that with MySQLdb api.
I already dig something about this issue like here but without success.
My query is like this:
create temporary table tmp1
select * from table1;
alter tmp1 add index(somefield);
create temporary table tmp2
select * from table2;
select * from tmp1 inner join tmp2 using(somefield);
This returns immediatly an empty result set. If i go to the mysql client and do a show full processlist i can see my queries executing. They take some minutes to complete.
Why cursor returns immediatly and don't wait to query to run.
If i try to run another query i have a "Commands out of sync; you can't run this command now"
I already tried to put my connection with autocommit to True
db = MySQLdb.connect(host='ip',
user='root',
passwd='pass',
db='mydb',
use_unicode=True
)
db.autocommit(True)
Or put every statement in is own cursor.execute() and between them db.commit() but without success too.
Can you help me to figure what is the problem? I know mysql don't support transactions for some operations like alter table, but why the api don't wait until everything is finished like it does with a select?
By the way i'm trying to do this on a ipython notebook.
I suspect that you're passing your multi-statement SQL string directly to the cursor.execute function. The thing is, each of the statements is a query in its own right so it's unclear what the result set should contain.
Here's an example to show what I mean. The first case is passing a semicolon set of statements to execute which is what I presume you have currently.
def query_single_sql(cursor):
print 'query_single_sql'
sql = []
sql.append("""CREATE TEMPORARY TABLE tmp1 (id int)""")
sql.append("""INSERT INTO tmp1 VALUES (1)""")
sql.append("""SELECT * from tmp1""")
cursor.execute(';'.join(sql))
print list(cursor.fetchall())
Output:
query_single_sql
[]
You can see that nothing is returned, even though there is clearly data in the table and a SELECT is used.
The second case is where each statement is executed as an independent query, and the results printed for each query.
def query_separate_sql(cursor):
print 'query_separate_sql'
sql = []
sql.append("""CREATE TEMPORARY TABLE tmp3 (id int)""")
sql.append("""INSERT INTO tmp3 VALUES (1)""")
sql.append("""SELECT * from tmp3""")
for query in sql:
cursor.execute(query)
print list(cursor.fetchall())
Output:
query_separate_sql
[]
[]
[(1L,)]
As you can see, we consumed the results of the cursor for each query and the final query has the results we expect.
I suspect that even though you've issued multiple queries, the API only has a handle to the first query executed and so immediately returns when the CREATE TABLE is done. I'd suggest serializing your queries as described in the second example above.
I'm using MysqlDB. Does it provide a way to execute multiple SELECT queries like mysqli_multi_query does? If not, is there a python library that would allow that?
There is executemany, but that's not what I'm looking for. I'm working with Sphinx and trying to get its batch queries to work.
I spent some time to dig in the source code of MySQLdb and the answer is YES you can do multiple queries with it:
import MySQLdb
db = MySQLdb.connect(user="username", db="dbname")
cursor = db.cursor()
batch_queries = '''
SELECT * FROM posts WHERE id=1;
SELECT * FROM posts WHERE id=2;
'''
cursor.execute(batch_queries)
print cursor.fetchone()
while cursor.nextset(): # iterate to next result set if there is any
print cursor.fetchone()
cursor.close()
Tested successfully in my localhost. Hope it helps.
I have a database containing a list of status updates and time stamp.
by executing the following python script
import sqlite3 as lite
import sys
con = lite.connect('Status.db')
with con:
cur = con.cursor()
cur.execute("SELECT * FROM Status")
print "The Status database now contains:"
for row in cur:
print row
results in the output:
The Status database now contains:
(1333155451.8815, u'message')
(1333155469.205055, u'message1')
(1333155473.496727, u'message2')
However, as the database grows, i wish to only view the latest, say, 10 messages. With the option of viewing older messages.
Could anyone give me some tips on how i would go about doing that?
Thanks
You can use ORDER BY and LIMIT, aod use the DESC keyword to show in reverse timestamp order:
SELECT * FROM <yourtable> ORDER BY <timestampcol> DESC LIMIT 10
(For future reference: When you're asking about how to query your data, you should post information about your schema, like table names, column names, and datatypes. It makes it much easier to post an answer containing the actual query.)
Look at ORDER BY and LIMIT clausuls. Those should do the trick :)