I'm using petl package to build an ETL pipeline from Python 2.7 to MySQL5.6
My db connector is MySQLdb (mysql-python).
The following code fails to execute:
import MySQLdb as mdb
import petl as etl
con = mdb.connect(host = '127.0.0.1', user = '<someuser>', passwd = '<somepass>')
cur = con.cursor() # get the cursor
cur.execute('DROP SCHEMA IF EXISTS petltest')
cur.execute('CREATE SCHEMA petltest')
cur.execute('USE petltest')
dat = [{'id':1,'name':'One'},
{'id':2,'name':'Two'},
{'id':3,'name':'Three'}]
table = etl.fromdicts(dat) # petl object
etl.todb(table,cur,'table',schema='petltest',create=True)
The error code is:
ProgrammingError: (1064, '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 \'"table" (\n\tid INTEGER NOT NULL, \n\tname
VARCHAR(5) NOT NULL\n)\' at line 1')
This error occurs also when trying to create table separately or running petl.appenddb
How can I fix it / overcome the issue?
Thanks
Apparently the problem was the quotes style that PETL use.
If you run:
cur.execute('SET SQL_MODE=ANSI_QUOTES')
before petl sql (petl.todb()) statements it executes well.
Related
I am trying to create a Database on my PC using python and SQLite 3 using the code below, however when I attempt to run I get this error
conn = sqlite3.connect('test_database')
sqlite3.OperationalError: unable to open database file
I'm quite new to this, so sorry if this is a simple mistake. Could someone tell me how i can fix this?
import sqlite3
conn = sqlite3.connect('test_database')
c = conn.cursor()
c.execute('''
CREATE TABLE IF NOT EXISTS bot_memory
([id] INTEGER PRIMARY KEY, [dateandtime] datetime, [task] varchar, [result] varchar)
''')
conn.commit()
conn.close()
The problem was I needed to install SQLite and SQLite Studio. Then, I can create and manage the Databases on my pc.
I want to write a program in Python to control MySQL database, but the following error occurs
pymysql.err.ProgrammingError: (1064, "You have an error in your SQL syntax; chec
k the manual that corresponds to your MySQL server version for the right syntax
to use near 'Code,type, number,Whether )VALUES ('cl8k-k520-fg45-d4sa-sd9k','n' at line 1")
Codeļ¼
import pymysql
db = pymysql.connect("localhost", "root", "root", "test")
cursor = db.cursor()
sql = "INSERT INTO BOOKS(Activation Code, \
type, number,Whether ) \
VALUES (%s,%s,%s,%s)"
data = [("cl8k-k520-fg45-d4sa-sd9k",'n','10','n'),
("jhg6-58jh-gh8o-8uik-7jk8",'c','20','n'),
("x5hg-gf5h-4hj5-g4h5-fh5t",'y','999','n'),
("fg8j-h584-fd4g-fg4d-fgg4",'n','1','y'),
("4jk4-5kl4-4klk-4lji-4ghj",'e','6','n'),
]
cursor.executemany(sql,data)
db.close()
This is my first time to ask questions on this platform. I hope to get answers
enclose Activation Code as 'Activation Code'
I'm trying to create procedure on Teradata using turbodbc.
There is my sample code:
from turbodbc import connect
con = connect(dsn="Teradata")
cur = con.cursor()
cur.execute("""
create procedure dev.test_procedure (
) sql security invoker
begin
delete dev.test_table;
end;
""")
And got this error:
DatabaseError: ODBC error
state: 42000
native error code: -3706
message: [Teradata][ODBC Teradata Driver][Teradata Database] Syntax error:
Invalid SQL Statement.
But the same code works with no errors in Teradata SQL Assistant. What's wrong?
Possible solution is use teradatasql. This sample works fine:
import teradatasql
with teradatasql.connect(
'{"host":"my_host","user":"my_user","password":"my_password"}'
) as con:
with con.cursor() as cur:
cur.execute("""
create procedure dev.test_procedure (
) sql security invoker
begin
delete dev.test_table ;
end ;
""")
But, it will be good if someone knows solution with turbodbc.
I am trying to get table structure using python from sqlite3 (.db) database. for that I am using the below code, but it is giving syntax error any help?
import sqlite3
conn = sqlite3.connect('Db-IMDB.db')
cursor = conn.cursor()
cursor.execute('DESCRIBE Movie')
It's not MySQL, but sqlite3. In Python API you can retrieve information on table from this statement:
table_info = cursor.execute("SELECT name FROM sqlite_master WHERE type='table';").fetchall()
I'm trying to set up a small database for a simple card game using mysql.connector 8.0 in jupyter notebook, yet I can't execute any queries, it says that the syntax is wrong even though it's all good.
That's the code:
'''
import random
import collections
import mysql.connector
conn = mysql.connector.connect(
host='localhost',
user='root',
passwd='*****',
auth_plugin='mysql_native_password',
database='CardGame'
)
cur = conn.cursor()
query = "CREATE TABLE cards (id int PRIMARY KEY AUTO_INCREMENT,rank VARCHAR(1),suit VARCHAR(10))"
cur.execute(query)
conn.commit()
'''
That's the error I keep getting:
ProgrammingError: 1064 (42000): 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 'rank VARCHAR(1), suit VARCHAR(10))' at line 1
I don't have an access to a MySQL database at the moment but your create table has RANK as a column name which is also a keyword. Can you change that to something else and try it out?