json to mysql via pandas and pymysql - python

I'm currently working on a real-time json via python
my idea :
- extract and wrangle json via pandas python (did it : about 3720 x 15 df)
- convert dataframe to tables in mysql (using pymysql on windows)
my code :
dfc=dfc.values.tolist()
connection = pymysql.connect(host='localhost',
user='root',
password='',
db="decaux",
charset='utf8mb4')
with connection.cursor() as cursor:
for rows in dfc:
sql7="insert into contracts(id,contract_name,commercial_name) values(%s, %s, %s)"
data = (rows[0],rows[1],rows[2])
cursor.execute(sql7,data)
but nothing happen...
what did I do wrong ?
Is there a better idea ?

Related

Mysql cursor is not fetching any data

I want to make one SELECT query from python to mysql DB.
But I'm getting an empty list when I call cursor.fetchall()
I have already tested the connection and the query with DBeaver, and it works fine.
I tried following the tutorials on https://dev.mysql.com but didn work.
Here is my function:
import mysql.connector
from mysql.connector import connect
def execute_query_2(self,query):
connection = connect(
host="config.host",
database="config.db_name",
user="config.db_user",
password="config.db_user"
)
print(connection)
cursor = connection.cursor()
cursor.execute(query)
result = cursor.fetchall()
print(result)
for row in result:
print(row)
The print(connection) gives me mysql.connector.connection_cext.CMySQLConnection object at 0x7f6a725bfca0.
Also the query is being loaded successfully as 'SELECT * from occUserManager.addressInformation'.
The result for this should bring 44 rows.
Any help is more than welcome.

How to Make SQL Table Show on Pycharm

I have a postgres db running on docker. I am able to access this db via my sql client Dbeaver and when I run select statements I see the expected results.
I would like to be able to query this db via a Python script and after some searching found psycopg2 package.
When I run the code below it 'looks' like it's successful, the conn and cursor objects appear as a variables.
import pandas as pd
import psycopg2
# connect to db
conn = psycopg2.connect(
host="localhost",
database="postgres",
user="postgres",
password="example")
# create a cursor
cur = conn.cursor()
However, when trying to query the db using cur.connect(), , variable ex_data is None. This exact same query via my sql client returns a table of data.
ex_data = cur.execute('select * from myschema.blah limit 10;')
How can I query my db via Python using psycopg2? Desired result wold be a data frame with the result set from the query string above.

What is wrong with this SQL statement in Python?

I am using Python and a MySQL database and am attempting to itterate through rows in a CSV file and insert them in my database. I have the following:
import mysql.connector
import pandas as pd
mydb = mysql.connector.connect(
host="localhost",
user="root",
passwd="root",
database="mydb")
cursor = mydb.cursor()
cursor.execute("SET FOREIGN_KEY_CHECKS=0")
csv_data = pd.read_csv("file path")
sql = "INSERT INTO table (ID, SecondID, StartDate, EndDate) VALUES (%s, %s, %s, %s)"
for index, row in csv_data.iterrows():
cursor.execute(sql, row)
cursor.execute("SET FOREIGN_KEY_CHECKS=1")
mydb.commit()
cursor.close()
mydb.close()
I can't see what's wrong with the SQL.
Getting the following error:
You have an error in your SQL syntax; check the manual that
corresponds to your MySQL server version for the right syntax to use
near '%s, %s, %s, %s)'
NOTE - The rest of the code seems to work okay and the SQL works fine if I insert specific values but when I try to use the %s construct it fails yet other responses I have seen appear to recommend this as the correct syntax.
Please help- what am I doing wrong?
I think you better use pandas to_sql function.
I'm not sure whether mysql.connector works so i'll use sqlalchemy.
It looks like that:
ENGINE = sqlalchemy.create_engine('mysql+pymysql://root:root#localhost:3306/mydb')
with ENGINE.connect() as connection:
ENGINE.execute("SET FOREIGN_KEY_CHECKS=0")
csv_data.to_sql('table_name', connection, if_exists='append', index=False)
ENGINE.execute("SET FOREIGN_KEY_CHECKS=1")
Look like that the problem is that you are invoking the query without scape the value.
The execute function its getting a class and not an array
for index, row in csv_data.iterrows():
cursor.execute(sql, row)
You should generate an array with all the values and then invoke the query.
Something like:
for index, row in csv_data.iterrows():
params = map(lambda x : x.value, row)
cursor.execute(sql,params)
Be carefull, the size of the array has to be the same size as the values params.
In this case 4
Thank you that was very helpful, I made one minor change and it works perfectly now. Here is the final solution I used:
import pandas as pd
import sqlalchemy
engine = sqlalchemy.create_engine('mysql+pymysql://root:root#localhost:3306/mydb')
csv_data = pd.read_csv("file path")
engine.execute("SET FOREIGN_KEY_CHECKS=0")
with engine.connect() as connection:
csv_data.to_sql('table', connection, if_exists='append', index=False)
engine.execute("SET FOREIGN_KEY_CHECKS=1")

Export a Dataframe into MSSQL Server as a new Table

I have written a Code to connect to a SQL Server with Python and save a Table from a database in a df.
from pptx import Presentation
import pyodbc
import pandas as pd
cnxn = pyodbc.connect("Driver={ODBC Driver 11 for SQL Server};"
"Server=Servername;"
"Database=Test_Database;"
"Trusted_Connection=yes;")
df = pd.read_sql_query('select * from Table1', cnxn)
Now I would like to modify df in Python and save it as df2. After that I would like to export df2 as a new Table (Table2) into the Database.
I cant find anything about exporting a dataframe to a SQL Server. you guys know how to do it?
You can use df.to_sql() for that. First create the SQLAlchemy connection, e.g.
from sqlalchemy import create_engine
engine = create_engine("mssql+pyodbc://scott:tiger#myhost:port/databasename?driver=SQL+Server+Native+Client+10.0")
See this answer for more details the connection string for MSSQL.
Then do:
df.to_sql('table_name', con=engine)
This defaults to raising an exception if the table already exists, adjust the if_exists parameter as necessary.
This is how I do it.
# Insert from dataframe to table in SQL Server
import time
import pandas as pd
import pyodbc
# create timer
start_time = time.time()
from sqlalchemy import create_engine
df = pd.read_csv("C:\\your_path\\CSV1.csv")
conn_str = (
r'DRIVER={SQL Server Native Client 11.0};'
r'SERVER=ServerName;'
r'DATABASE=DatabaseName;'
r'Trusted_Connection=yes;'
)
cnxn = pyodbc.connect(conn_str)
cursor = cnxn.cursor()
for index,row in df.iterrows():
cursor.execute('INSERT INTO dbo.Table_1([Name],[Address],[Age],[Work]) values (?,?,?,?)',
row['Name'],
row['Address'],
row['Age'],
row['Work'])
cnxn.commit()
cursor.close()
cnxn.close()
# see total time to do insert
print("%s seconds ---" % (time.time() - start_time))

Python MySQLdb doesn't return all the data from the database

I'm using the Python package MySQLdb to fetch data from a MySQL database. However, I notice that I can't fetch the entirety of the data.
import MySQLdb
db = MySQLdb.connect(host=host, user=user, passwd=password)
cur = db.cursor()
query = "SELECT count(*) FROM table"
cur.execute(query)
This returns a number less than what I get if I execute the exact same query in MySQL Workbench. I've noticed that the data it doesn't return is the data that was inserted into the database most recently. Where am I going wrong?
You are not committing the inserted rows on the other connection.

Categories