psycopg2 not actually inserting data - python

I need to insert JSON data from tornado to postgres, so here's test like this:
from psycopg2 import connect
conn = connect("user='pguser' host='localhost' dbname='pgdb' password='pgpass'")
cursor = conn.cursor()
data = '[{"id":"sdf","name":"wqe","author":"vb"}]'
for row in eval(data):
print row
cursor.execute("""INSERT INTO books(id,name,author) VALUES('%s','%s','%s')""" % \
(row['id'], row['name'], row['author'])
)
>>> cursor.execute("SELECT * FROM books")
>>> cursor.fetchall()
[('sdf', 'wqe', 'vb')]
>>>
$> psql -d pgdb -U pguser -W
Password for user pguser:
psql (9.1.6)
Type "help" for help.
pgdb=> select * from books;
id | name | author
----+------+--------
(0 rows)
As you can see after doing select in python shell, there's some data, but in psql there's
0 rows! What may I be doing wrong?
Python 2.7.2+

You didn't commit the transaction.
Psycopg2 opens a transaction automatically, and you must tell it to commit in order to make the data visible to other sessions.
See the psycopg2 FAQ and the connection.commit() method.

Just had the same perplexing issue. To put options together:
as #Craig Ringer writes after cursor.execute you can run connection.commit
cursor.execute('INSERT INTO table VALUES(DEFAULT, %s)', email)
...
connection.commit()
OR
after connect set autocommit
connection = connect("user='pguser' host='localhost' dbname='pgdb' password='pgpass'")
connection.autocommit = True
OR
use set_session to set autocommit
connection = connect("user='pguser' host='localhost' dbname='pgdb' password='pgpass'")
connection.set_session(autocommit=True)
All worked for me.

Related

MySQL connection with Python through PythonAnywhere

I want to connect to MySql database using Python through PythonAnywhere, without creating a Flask/Django application.
I have seemingly managed to connect through MySQLdb, using the code below, but I do not receive a response when I run the code. Any solutions?
import MySQLdb
db = MySQLdb.connect(
host = "myuser.mysql.pythonanywhere-services.com",
user = "myuser",
passwd = XXX,
db = "myuser$db_name"
)
cursor = db.cursor()
cursor.execute("SELECT * FROM table_name")
for x in cursor:
print(x)
cursor.close()
db.close()
You retrieve all rows in the table, without error.
cursor.execute("SELECT * FROM table_name")
for x in cursor:
print(x)
Yet you see no output. This is normal for a table that contains zero rows.
Consider doing one or more INSERTs, and a COMMIT,
prior to the query.

My code doesn't actually insert into the database

I don't run into any error messages anymore but when i refresh my database nothing is actually injected? using psycopg2 and pgadmin4
import psycopg2 as p
con = p.connect("dbname =Feedbacklamp user =postgres password= fillpw host=localhost port=5432")
cur = con.cursor()
sql = "INSERT INTO audiolevels(lokaalnummer,audiolevel,tijdstip) VALUES (%s,%s,%s)"
val = "100"
val1 = 100
val2 = "tijdstip"
cur.execute(sql,(val,val1,val2))
con.commit
cur.close
con.close
The values to be inserted into my pgadmin sql database
con.commit() should be a function call I think is your problem. You are missing the parentheses which treats it as member access instead of a function call. This also goes for the other methods cur.close() and con.close()

pymssql can connect but does not return any results

I am connecting to an MS SQL server with pymssql. I can connect by tsql -H ip -p 1433 -U xx - p xx and by jupyter notebook. The connection does not return any errors.
However, I tried many queries with pymssql but none of them returned results.
For example, cursor.execute('SELECT * FROM INFORMATION_SCHEMA.TABLES ')
What should I check now?
As mentioned in the "Iterating through results" section of the pymssql examples, you can simply iterate through the rows of the result set like this:
crsr = conn.cursor()
crsr.execute("SELECT * FROM INFORMATION_SCHEMA.TABLES")
for row in crsr:
print(row)
Or, to use a more standard DB-API approach:
crsr = conn.cursor()
crsr.execute("SELECT * FROM INFORMATION_SCHEMA.TABLES")
for row in crsr.fetchall():
print(row)

Cannot get the latest DB data by "select" sql command, after updated that data by "update" command by another mysql connection

there are two mysql connection in my python script. example:
conn1 = mdb.connect(server, user, pw, db)
conn2 = mdb.connect(server, user, pw, db)
#1. then, I execute "select" sql command to select table A by conn1,
#2. after that, I execute "update" sql command to update table A by conn2,
#3. finally, I execute "select" sql command again to select table A by conn1,
but finally, the result of #3 is same as #1; however, after #2, i saw the data is updated in mysql workbench.
Is anybody know why #3 cannot get the latest data?
following is my python codes:
import MySQLdb as mdb
import time
conn1 = mdb.connect(SERVER, USER, PASSWORD, DB)
cur1 = conn.cursor()
count1 = cur.execute("SELECT trigger_time FROM trigger_set WHERE id=1")
data1 = cur.fetchall()
cur1.close()
print data1
conn2 = mdb.connect(SERVER, USER, PASSWORD, DB)
cur2 = conn2.cursor()
cur2.execute("update trigger_set set trigger_time = '2013/8/30 17:15' where id=1")
conn2.commit()
cur2.close()
cur1 = conn.cursor()
count1 = cur.execute("SELECT trigger_time FROM trigger_set WHERE id=1")
data1 = cur.fetchall()
print data1
Things would work if you commit your conn1 before selecting again.. or just enable auto_commit to True.
conn1.commit()
This has been talked about here: http://sourceforge.net/p/mysql-python/discussion/70461/thread/efea588e
BTW your code contains conn and cur variables which have not been defined..
I think the problem lies with reusing the same cursor `cur1' for step 1 and 3.Try using a different cursor or connection for your last select too.

How to retrieve table names in a mysql database with Python and MySQLdb?

I have an SQL database and am wondering what command you use to just get a list of the table names within that database.
To be a bit more complete:
import MySQLdb
connection = MySQLdb.connect(
host = 'localhost',
user = 'myself',
passwd = 'mysecret') # create the connection
cursor = connection.cursor() # get the cursor
cursor.execute("USE mydatabase") # select the database
cursor.execute("SHOW TABLES") # execute 'SHOW TABLES' (but data is not returned)
now there are two options:
tables = cursor.fetchall() # return data from last query
or iterate over the cursor:
for (table_name,) in cursor:
print(table_name)
SHOW tables
15 chars
show tables will help. Here is the documentation.
It is also possible to obtain tables from a specific scheme with execute the single query with the driver below.
python3 -m pip install PyMySQL
import pymysql
# Connect to the database
conn = pymysql.connect(host='127.0.0.1',user='root',passwd='root',db='my_database')
# Create a Cursor object
cur = conn.cursor()
# Execute the query: To get the name of the tables from a specific database
# replace only the my_database with the name of your database
cur.execute("SELECT table_name FROM information_schema.tables WHERE table_schema = 'my_database'")
# Read and print tables
for table in [tables[0] for tables in cur.fetchall()]:
print(table)
output:
my_table_name_1
my_table_name_2
my_table_name_3
...
my_table_name_x

Categories

Resources