I was trying to use the python connector code given in the MySQL documentation and test it on a small database already created, but it aborts. The code is just supposed to connect to the db and add a new email adress.
import mysql.connector
from mysql.connector import errorcode
cnx = mysql.connector.connect(user='root', password='pwd', host='localhost', database='db')
cursor = cnx.cursor()
add_email = ("INSERT INTO employee(MID, Email) VALUES (%s,%s)")
email_details = (NULL, "a#a.de")
cursor.execute(add_email, email_details)
cnx.commit()
input("data entered successfully")
cnx.close()
By setting breakpoints I found out that the problem probably lies in the cursor.execute() statement. (I used Null as the first %s since MID is Auto Incrementing btw)
To solve this problem NULL (for the autoincrementing "MID") needs to be replaced with None.
I have a mysql database that I am currently trying to connect python to. I have successfully created a table with python but when it comes to inserting data in the table my code does not work.
import pymysql
conn = pymysql.connect(
host = Admin.Host,
user = Admin.User,
password= Admin.Password,
db = "thesystem"
)
a = conn.cursor()
sql ="INSERT INTO `a`(`try`) VALUES ('testing')"
a.execute(sql)
I have tried this exact command in the Mysql command Line client and it worked.
INSERT INTO `a`(`try`) VALUES ('testing');
I have also managed to create a table with this same code. I can't figure out why the code runs successfully but does not work.
The resolution is that pymysql requires that you call conn.commit() after making a change. By default, pymysql is not autocommit.
Code example at https://github.com/PyMySQL/PyMySQL says:
. . .
# connection is not autocommit by default. So you must commit to save
# your changes.
connection.commit()
. . .
I am trying to connect to SQLITE database and it would seem that the code does create the database, but however it does not create the tables nor does it insert anything in them (most probably because they were never created)
I have SQLite DB Browser and in it I open the database but lack any tables or anything at all. Also I see my database in the folder next to my project.
I have the following code:
import sqlite3
conn = sqlite3.connect("dfg.db")
c = conn.cursor()
def create_tabe():
c.execute('CREATE TABLE IF NOT EXISTS tabl(city TEXT, temp REAL)')
def data_entry():
c.execute("INSERT INTO tabl VALUES ('dasfsd', 32434)")
conn.commit()
c.close()
conn.close()
I am able to connect Python 3.4 and Postgres but I am the query is not successfully getting executed. For e.g, the table below is not getting created
import psycopg2
from psycopg2 import connect
try:
conn = psycopg2.connect("dbname='postgres' user='postgres' host='localhost' password='postgres'")
print("Database connected!")
cur = conn.cursor()
cur.execute("""CREATE TABLE DEPARTMENT(
ID INT PRIMARY KEY NOT NULL,
DEPT CHAR(50) NOT NULL,
EMP_ID INT NOT NULL
)""")
except:
print("I am unable to connect to the database")
Just add
conn.commit()
after you've run the execute.
Relational databases have the concept of transaction, which happen (if at all) "atomically" (all-or-none). You need to commit a transaction to actually make it take place; until you've done that, you keep the option to rollback it instead, to have no changes made to the DB if you've found something iffy on the way.
I'm trying to add some data to my database. Code works good without error. But when i'm checking my database i see that there is nothing new. Where is error?
import MySQLdb as mdb
con = #connection settings
cur = con.cursor()
cur.execute("""INSERT INTO myData(id,city,district,testval,
test_date,property_type,price,testvar,title,
description,number_of_tester,number_of_test,
test_age,test_number,number_of_tests,heating,test_type,
applicable,usage_type,test_profile,test,
house_type,latitude,longitude,test_name,test_phones,test_profile,
test_desc,test_title)
VALUES (123456,'Baku','Ahmedli rn','Ahmadli',
'2010-11-10 12:00:00','',1,0,'','','',0,'','','','','','','','','','',0,0,'','','','','')""")
con.close()
Problem Solved, MySQL in Python needs con.commit() at the end to add all this to datebase.