Optimising mysql insert query via python - python

db = MySQLdb.connect(host="xxx.xx.xx.x",
user="xxx",
passwd="xxx",
db="xxxx")
for loop on json data:
cursor = db.cursor()
cursor.execute('Insert Query')
db.commit()
db.close()
Would it be possible for me to improve this query? I'm considering doing multiple cursor.execute before db.commit()
I'm unsure how db.commit() works and the importance of it.
I'm basically looping a json data and inserting it with a loop. I cannot avoid having multiple inserts.

Depending on how json_data is structured you should be able to use .executemany():
db = MySQLdb.connect(host="xxx.xx.xx.x",
user="xxx",
passwd="xxx",
db="xxxx")
cursor = db.cursor()
cursor.executemany('Insert Query',json_data)
db.commit()
cursor.close()
db.close()

Related

My SQL with Python: Select the row with the highest value and change the value there

I have already searched for several solutions here and tried to get a working code. Everything works except for the where query.
In the where query I search for the highest value (numeric). However, this does not really work...
Here is my code and the structure of the MySQL database.
Thanks!
import pymysql
conn = pymysql.connect(host='localhost', unix_socket='', user='root', passwd='pw', db='database')
cur = conn.cursor()
cur.execute("SELECT * FROM dose")
for r in cur:
curr = conn.cursor()
sql = """UPDATE dose
SET status = "printed"
WHERE id = SELECT GREATEST (status) FROM dose (status);"""
# print(sql)
try:
# Execute the SQL command
curr.execute(sql)
# Commit your changes in the database
conn.commit()
except:
# Rollback in case there is any error
conn.rollback()
curr.close()
cur.close()
conn.close()
My SQL Database
You have a lot of things wrong in your code.
You don´t use the results of your first select query, and the only thing that you do is iterate over the results to execute an UPDATE
Your update query is wrong
You should change it to:
import pymysql
conn = pymysql.connect(host='localhost', unix_socket='', user='root', passwd='pw', db='database')
curr = conn.cursor()
sql = """UPDATE dose
SET status = 'printed'
WHERE id = (SELECT max(status) FROM dose) """
try:
# Execute the SQL command
curr.execute(sql)
# Commit your changes in the database
conn.commit()
except:
# Rollback in case there is any error
conn.rollback()
curr.close()
conn.close()

How can I sort a database by date?

I am creating a Python app that will store my homework in a database (using PhpMyAdmin). Here comes my problem:
At this moment, I am sorting every input with an ID (1, 2, 3, 4...), a date (23/06/2018...), and a task (read one chapter of a book). Now I would like to sort them by the date because when I want to read what do I have to do. I would prefer to see what shall I do first, depending on when should I get it done. For example:
If I have two tasks: one 25/07/2018 and the other 11/07/2018, I would like to show the 11/07/2018 first, no matter if it was addead later than the 25/07/2018. I am using Python (3.6), pymysql and PhpMyAdmin to manage the database.
I have had an idea to get this working, maybe I could run a Python script every 2 hours, that sorts all the elements in the database, but I have no clue about how can I do it.
Now, I will show you the code that enters the values into a database and then it shows them all.
def dba():
connection = pymysql.connect(host='localhost',
user='root',
password='Adminhost123..',
db='deuresc',
charset='utf8mb4',
cursorclass=pymysql.cursors.DictCursor)
try:
with connection.cursor() as cursor:
# Create a new record
sql = "INSERT INTO `deures` (`data`, `tasca`) VALUES (%s, %s)"
cursor.execute(sql, (data, tasca))
# connection is not autocommit by default. So you must commit to save
# your changes.
connection.commit()
with connection.cursor() as cursor:
# Read a single record
sql = "SELECT * FROM `deures` WHERE `data`=%s"
cursor.execute(sql, (data,))
resultat = cursor.fetchone()
print('Has introduït: ' + str(resultat))
finally:
connection.close()
def dbb():
connection = pymysql.connect(host='localhost',
user='root',
password='Adminhost123..',
db='deuresc',
charset='utf8mb4',
cursorclass=pymysql.cursors.DictCursor)
try:
with connection.cursor() as cursor:
# Read a single record
sql = "SELECT * FROM `deures`"
cursor.execute(sql)
resultat = cursor.fetchall()
for i in resultat:
print(i)
finally:
connection.close()
Can someone help?
You don't sort the database. You sort the results of the query when you ask for data. So in your dbb function you should do:
SELECT * FROM `deures` ORDER BY `data`
assuming that data is the field with the date.

Python MySQL insert

I have a problem with an insert in python. All the attributes are correct and it gives no errors. Here is my code:
db = MySQLdb.connect(host="localhost", user="root", passwd="", db="ueberwachung") #Datenbank connection
cur = db.cursor()
cur.execute("INSERT INTO tereignisse VALUES('1002', '2016-12-02','18:34:15','/var/www/html/videos/2016-11-0103:21:13', '0')")
cur.close()
db.close()
Edit: Sorry I forgot the problem. So problem: It doesn't write anything into the table.
You need to commit the transaction before closing the connection:
db.commit()
cur.close()
db.close()

Python Postgres how to insert

try:
conn = psycopg2.connect("dbname='test1' user='postgres' host='localhost' password='123'")
cur = conn.cursor()
cur.execute("""INSERT INTO product_info (product_name) VALUES (%s)""", 'xxx')
except:
print "error happens"
The above is my code snippet, I have no trouble connecting to the database, but I have some problem inserting the value into it.
I execute the same query in postgres and it works, so i think it's a syntax problem.
Can someone show me what is the right way to do insertion?
cur.execute("""
insert into product_info (product_name) VALUES (%s)
""", ('xxx',))
conn.commit()
Notice that the value is passed to the method wrapped in an iterable.

How do I determine if the row has been inserted?

When using sqlite3 for python, how do I determine if a row has been successfully inserted into a table? e.g.
conn = sqlite3.connect("test.db")
c = conn.cursor()
c.execute("INSERT INTO TEST VALUES ('sample text')")
c.commit()
c.close()
If no exception was thrown when calling execute() or commit(), it was inserted when you called commit().
Committing a transaction successfully is a guarantee from the database layer that the insert was written to disk.
you can get all the rows and see if its in there with:
SELECT * FROM TEST
But SQLite will give you an error message if it didnt work.
you can count() rows before inserting and after inserting.
You could try something like this to have an error message:
try:
c.execute("INSERT INTO TEST VALUES ('sample text')")
except sqlite3.OperationalError, msg:
print msg
You should do the commit on connection made(db selected which is conn) not on cursor.
conn = sqlite3.connect("test.db")
c = conn.cursor()
c.execute("INSERT INTO TEST VALUES ('sample text')")
#commit the changes to db
conn.commit()
conn.close()
First You should do the commit on the connection object not the cursor i.e
conn.commit() not c.commit()
Then you can examine lastrowid on the cursor to determine if the insert was successful after conn.commit()
c.lastrowid

Categories

Resources