In order to delete the table named cars I created in python-mysql , I do
import MySQLdb as mdb
con = mdb.connect('localhost', 'priceapi', 'data', 'carpricedb');
cur = con.cursor()
cur.execute("DELETE FROM cars");
con.commit()
con.close()
But I get an error like this
Traceback (most recent call last):
File "delete.py", line 4, in <module>
cur.execute("DELETE FROM cars");
File "/usr/lib/python2.7/dist-packages/MySQLdb/cursors.py", line 174, in execute
self.errorhandler(self, exc, value)
File "/usr/lib/python2.7/dist-packages/MySQLdb/connections.py", line 36, in defaulterrorhandler
raise errorclass, errorvalue
_mysql_exceptions.OperationalError: (1205, 'Lock wait timeout exceeded; try restarting transaction')
My Table only has 78 rows. I also tried truncating the table. The result is the same.
I tried doing it directly in mysql , but then it doesnt execute the command and I had to kill it using Control+C. I searched a lot for the last 1 hour, but could not find the solution .
Any help please ?
Delete is very very very very slow SQL operation and i guess that the table is huge so it takes a lot of time to finish. It's best to drop and recreate a table if you want it clear (truncate is a 2nd best option).
It is also possible that some query is locking your table, you can clear that lock off by stopping and starting mysql again (or by finding which query is doing that and kill it).
Related
I have a problem inserting data into my database table. I'm using Python and MariaDB. Connection to database is open and tested, I'm able to query the database, but I can't nail the insert syntax. I've found two ways, but neither work.
insert = (
"INSERT INTO ksiazka (ISBN, tytul, autor, rok_wydania, ilosc stron)"
"VALUES (%s,%s,%s,%s,%s)"
)
dane = (ISBN, tytul, autor, rok_wydania, ilosc_stron)
cursor.execute(insert, dane)
or this way:
cursor.execute("INSERT INTO ksiazka (ISBN, tytul, autor, rok_wydania, ilosc stron) VALUES (%s,%s,%s,%s,%s)", (ISBN, tytul, autor, rok_wydania, ilosc_stron))
When executing I get this error:
Traceback (most recent call last): File
"C:\Users\jakub\AppData\Local\Programs\Python\Python36\lib\site-packages\mysql\connector\connection_cext.py",
line 377, in cmd_query
raw_as_string=raw_as_string)
_mysql_connector.MySQLInterfaceError: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server
version for the right syntax to use near 'stron)VALUES
('12345678','wertvfdg','3','1243','213')' at line 1
During handling of the above exception, another exception occurred:
Traceback (most recent call last): File
"C:/Users/jakub/PycharmProjects/biblioteka/sql_connector.py", line 57,
in
cursor.execute(insert, dane) File "C:\Users\jakub\AppData\Local\Programs\Python\Python36\lib\site-packages\mysql\connector\cursor_cext.py",
line 264, in execute
raw_as_string=self._raw_as_string) File "C:\Users\jakub\AppData\Local\Programs\Python\Python36\lib\site-packages\mysql\connector\connection_cext.py",
line 380, in cmd_query
sqlstate=exc.sqlstate) mysql.connector.errors.ProgrammingError: 1064 (42000): You have an error in your SQL syntax; check the manual
that corresponds to your MariaDB server version for the right syntax
to use near 'stron)VALUES ('12345678','wertvfdg','3','1243','213')' at
line 1
If your column name has a space in it, then it needs special handling and you must escape it:
INSERT INTO ksiazka (ISBN, tytul, autor, rok_wydania, `ilosc stron`) ...
This is why spaces in column names are annoying and should be avoided.
So I've got a .sql file that I want to build over to a .db file for sqllite3, but I'm getting a
sqlite3.OperationalError: near "ENGINE": syntax error
Any ideas why? Any help is appreciated.
import sqlite3
conn = sqlite3.connect('newdatabase.db')
f = open('olddatabase.sql','r')
sql = f.read()
conn.executescript(sql)
conn.close()
OUTPUT:
Traceback (most recent call last):
File "deleteme.py", line 6, in
conn.executescript(sql)
sqlite3.OperationalError: near "ENGINE": syntax error
(program exited with code: 1)
Divining based on ENGINE, you have a .sql file dumped by MySQL (CREATE TABLE ... ENGINE=InnoDB or similar), so it contains MySQL-specific extensions.
You'll have to either edit the .sql file to conform to SQLite's standards or figure out a way to convert it automatically to a more standard sequence of SQL instructions.
I am using a python module called phoenixdb to access phoenix which is SQL wrapper to query over HBase.
Here is my code snippet:-
import phoenixdb
database_url = 'http://localhost:8765/'
conn = phoenixdb.connect(database_url, autocommit=True)
cursor = conn.cursor()
cursor.execute("!table")
print cursor.fetchall()
cursor.close()
The phoenix query to list all the schemes and tables is !table or !tables.
But when I try passing the same in the execute function as shown above, I get the following error:-
Traceback (most recent call last):
File "phoenix_hbase.py", line 7, in <module>
cursor.execute("!table")
File "build/bdist.linux-x86_64/egg/phoenixdb/cursor.py", line 242, in execute
File "build/bdist.linux-x86_64/egg/phoenixdb/avatica.py", line 345, in prepareAndExecute
File "build/bdist.linux-x86_64/egg/phoenixdb/avatica.py", line 184, in _apply
File "build/bdist.linux-x86_64/egg/phoenixdb/avatica.py", line 90, in parse_error_page
phoenixdb.errors.ProgrammingError: ("Syntax error. Unexpected char: '!'", 601, '42P00', None)
Funny part is when I try to passing a different query, for example a select query, then script gets executed and produces result just fine.
Query:cursor.execute("select * from CARETESTING.EDR_BIN_SOURCE_3600_EDR_FLOW_SUBS_TOTAL limit 1")
Result:
[[2045,1023,4567]]
Is there any other format for passing !table which is equivalent to show tables in phoenixdb library's execute function which I am missing?
I tried looking up on the internet but unfortunately haven't come across anything helpful so far.
Thanks
!tables is sqlline grammar that can not be parsed by JDBC interface.
Here is the error:
gutschy#kiste:~/pizza/pizza_daten$ python datenimport3.py
Traceback (most recent call last):
File "datenimport3.py", line 20, in <module>
")
File "/usr/lib/python2.7/dist-packages/MySQLdb/cursors.py", line 174, in execute
self.errorhandler(self, exc, value)
File "/usr/lib/python2.7/dist-packages/MySQLdb/connections.py", line 36, in defaulterrorhandler
raise errorclass, errorvalue
_mysql_exceptions.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 ''Adressliste_forum1_v4.csv' INTO TABLE pizzeria_table FIE' at line 1")
gutschy#kiste:~/pizza/pizza_daten$
Here the datenimport3.py
#!/usr/bin/python
#-*- coding: utf-8 -*-
import MySQLdb as mdb
con = mdb.connect('localhost', 'user', 'passw', 'pizzadb2');
with con:
cur = con.cursor(mdb.cursors.DictCursor)
cur.execute(" 'Adressliste_forum1_v4.csv'\
INTO TABLE pizzeria_table \
FIELDS TERMINATED BY ',' \
ENCLOSED BY '\"' \
LINES TERMINATED BY '\\n' \
IGNORE 1 LINES \
(laden_name, vorwahl, telenr1, strasse, hausnr, \
ort, linkname1, linkname2, linkname3, forum_link, \
link2, link3, banner) \
")
Four month ago it works all fine, than I killed my debian 7 and now I'm on to bring it in the same way like before. I've add the last field "banner" new but I've done noting more.
You can sort through all that traceback chaff by looking for this:
...for the right syntax to use near ''Adressliste_forum1_v4.csv' INTO ...
On a 1064 error, MySQL presents, right after "use near", your statement starting with the place where its parse error was found.
Now, should not your statement start with
LOAD DATA INFILE 'Adressliste_forum1_v4.csv' INTO ...
?
I have a working python script which retrieves data from a sqlite3 db. I need to convert it to talk to a mysql db though, and here's the first line that is giving me grief:
PRODUCT_CODE_ID = "SELECT id FROM localsite_productcode WHERE localsite_productcode.product_code=?"
cur.execute(PRODUCT_CODE_ID,(product_code,))
When I try that with mysql I get the following error:
Traceback (most recent call last):
File "gen-csv.py", line 85, in <module>
cur.execute(PRODUCT_CODE_ID,(product_code,))
File "/usr/lib/pymodules/python2.6/MySQLdb/cursors.py", line 151, in execute
query = query % db.literal(args)
TypeError: not all arguments converted during string formatting
What am I doing wrong here?
I don't think the DB API for MySQL supports the ? as a place holder - just try %s instead.