mysql-python

How do I check if an insert was successful with MySQLdb in Python?

不羁岁月 提交于 2020-01-10 01:06:03
问题 I have this code: cursor = conn.cursor() cursor.execute(("insert into new_files (videos_id, filename, " "is_processing) values (%s,%s,1)"), (id, filename)) logging.warn("%d", cursor.rowcount) if (cursor.rowcount == 1): logging.info("inserted values %d, %s", id, filename) else: logging.warn("failed to insert values %d, %s", id, filename) cursor.close() Fun as it is, cursor.rowcount is always one, even though i updated my database to make the videos_id a unique key. That is, the insert fails

Python-MySQL : removing single quotes around variable values in query while running db.execute(str, vars)

时光毁灭记忆、已成空白 提交于 2020-01-07 06:54:46
问题 I am running this code def details(self, dbsettings, payload): res = None with UseDatabase(dbsettings) as db: sql = "select * from %(tablename)s where userid = %(userid)s" result = db.run_query_vals(sql, payload) res = result.fetchall() return res but get an error SQLError: 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 ''statuser' where userid = '14'' at line 1 The arguments being passed

Scrapy: MySQL Pipeline — Unexpected Errors Encountered

做~自己de王妃 提交于 2020-01-06 14:44:13
问题 I'm getting a number of errors, depending upon what is being inserted/updated. Here is the code for processing the item: def process_item(self, item, spider): try: if 'producer' in item: self.cursor.execute("""INSERT INTO Producers (title, producer) VALUES (%s, %s)""", (item['title'], item['producer'])) elif 'actor' in item: self.cursor.execute("""INSERT INTO Actors (title, actor) VALUES (%s, %s)""", (item['title'], item['actor'])) elif 'director' in item: self.cursor.execute("""INSERT INTO

python-mysqldb without transactions

别说谁变了你拦得住时间么 提交于 2020-01-06 04:19:06
问题 I'm reading about how do transactions work in python's MySQLdb . In this tutorial, it says that: In Python DB API, we do not call the BEGIN statement to start a transaction. A transaction is started when the cursor is created. So a following line: cur = con.cursor() starts a transaction implicitly. It also says, that: We must end a transaction with either a commit() or a rollback() method. Do I understand it correctly, that MySQLdb uses transactions always and there's no way to turn this

change CLIENT_FOUND_ROWS flag in django for mysql-python (MySQLdb)?

偶尔善良 提交于 2020-01-05 10:26:26
问题 I have a problem with MySQL 5.5 INSERT ... ON DUPLICATE KEY UPDATE rows effected mismatch cursor.rowcount in a normal Django project According to doc: For INSERT ... ON DUPLICATE KEY UPDATE statements, the affected-rows value per row is 1 if the row is inserted as a new row, 2 if an existing row is updated, and 0 if an existing row is set to its current values. If you specify the CLIENT_FOUND_ROWS flag , the affected-rows value is 1 (not 0) if an existing row is set to its current values. So

Create MySQL Database in Python using the %s operator

匆匆过客 提交于 2020-01-05 04:00:09
问题 Trying to create a database where the name is given through the %s operator. import mysql.connector, MySQLdb db_name='SomeString' #create connection to mysql mydb=mysql.connector.connect(host="localhost",user="root") #init cursor mycursor=mydb.cursor() #create database mycursor.execute("CREATE DATABASE (%s)", (db_name)) This is the error msg: mysql.connector.errors.ProgrammingError: 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server

Python-MySQLdb, how do you access the exception error-code in `OperationalError`?

僤鯓⒐⒋嵵緔 提交于 2020-01-05 02:29:55
问题 I need to catch a specific OperationalError exception. The exception text uses the error-code 2006. The library defines the error-codes at MySQLdb.constants.CR.SERVER_GONE_ERROR = 2006 . How do you get the error-code from the exception? When I check the MySQLdb._mysql_exceptions, there is a definition of the OperationalError exception but it has no constructor or description of how to access the exception error code. 回答1: You can catch the error number like the following: try: # Adding field

python MySQLDb insert with prepared Statements [duplicate]

走远了吗. 提交于 2020-01-04 13:13:28
问题 This question already has answers here : MySQL parameterized queries (7 answers) Closed 3 years ago . I have a table with fields TABLE_PASTE( user_text longtext NOT NULL, number integer NOT NULL ) I am trying to insert a row in this table TABLE_PASTE from python using MySQLDb driver. text="large_text" value = 1 cursor.execute("Update TABLE_PASTE set user_text = ? where number = ?",(text,value)) Am getting this error query = query % db.literal(args) TypeError: not all arguments converted

python MySQLDb insert with prepared Statements [duplicate]

拟墨画扇 提交于 2020-01-04 13:13:12
问题 This question already has answers here : MySQL parameterized queries (7 answers) Closed 3 years ago . I have a table with fields TABLE_PASTE( user_text longtext NOT NULL, number integer NOT NULL ) I am trying to insert a row in this table TABLE_PASTE from python using MySQLDb driver. text="large_text" value = 1 cursor.execute("Update TABLE_PASTE set user_text = ? where number = ?",(text,value)) Am getting this error query = query % db.literal(args) TypeError: not all arguments converted

Commands out of sync you can't run this command now

大城市里の小女人 提交于 2020-01-04 06:15:22
问题 I am trying to create some tables using mysqldb. The issue is that when executing the python script db.py mysql throws the error: _mysql_exceptions.ProgrammingError: (2014, "Commands out of sync; you can't run this command now") db.py: import MySQLdb import MySQLdb.cursors def init_db(): db_conn = get_db_conn() cursor = db_conn.cursor() with open("tables.sql", 'r') as f: cursor.execute(f.read()) def get_db_conn(): return MySQLdb.connect( host="localhost", user="root", passwd="secretcat", db=