mysql-python

Python MySQLdb issues (TypeError: %d format: a number is required, not str)

有些话、适合烂在心里 提交于 2019-11-27 06:47:24
I am trying to do the following insert operation: cursor.execute(""" insert into tree (id,parent_id,level,description,code,start,end) values (%d,%d,%d,%s,%s,%f,%f) """, (1,1,1,'abc','def',1,1) ) The structure of my MYSQL table is: id int(255), parent_id int(255), level int(11), description varchar(255), code varchar(255), start decimal(25,4), end decimal(25,4) However when I run my program, I get the error " File "/usr/lib/pymodules/python2.6/MySQLdb/cursors.py", line 151, in execute query = query % db.literal(args) TypeError: %d format: a number is required, not str" Keith The format string

What is PyMySQL and how does it differ from MySQLdb? Can it affect Django deployment?

你。 提交于 2019-11-27 06:44:15
I just solved some problems in my Django 1.3 app by using PyMySQL instead of MySQLdb. I followed this tutorial on how to make the switch: http://web-eng-help.blogspot.com/2010/09/install-mysql-5-for-python-26-and.html Now I want to know what PyMySQL actually is and how it is different from MySQLdb. I am using it on localhost and will then upload it to some hosting. Is it fine to use PyMySQL on localhost and on hosting whatever they provide? Since I have changed "MySQLdb" in base.py and introspection.py to "PyMySQL", will I need to upload it to the server after changing these files? Or as it is

MySQLdb.cursor.execute can't run multiple queries

安稳与你 提交于 2019-11-27 06:39:52
问题 We're trying to run SQL files containing multiple insert statements as a single query, but it seems rollback fails when any of the statements contain an error. MySQLd configuration: sql_mode = STRICT_ALL_TABLES default-storage-engine = innodb Python code: from contextlib import closing import MySQLdb database_connection = MySQLdb.connect(host="127.0.0.1", user="root") with closing(database_connection.cursor()) as cursor: database_connection.begin() cursor.execute('DROP DATABASE IF EXISTS db

Print the actual query MySQLdb runs?

▼魔方 西西 提交于 2019-11-27 06:39:33
I'm looking for a way to debug queries as they are executed and I was wondering if there is a way to have MySQLdb print out the actual query that it runs, after it has finished inserting the parameters and all that? From the documentation, it seems as if there is supposed to be a Cursor.info() call that will give information about the last query run, but this does not exist on my version (1.2.2). This seems like an obvious question, but for all my searching I haven't been able to find the answer. Thanks in advance. xitrium We found an attribute on the cursor object called cursor._last_executed

Python MySQLdb execute table variable [duplicate]

ぃ、小莉子 提交于 2019-11-27 05:25:22
This question already has an answer here: Python sqlite3 parameterized drop table 1 answer I'm trying to use a variable for a table name. I get the error "... near ''myTable'' at line 1 I must not be escaping this right. The double '' in the error seems to be a clue, but I don't get it. db = MySQLdb.connect("localhost","user","pw","database" ) table = "myTable" def geno_order(db, table): cursor = db.cursor() # prepare a cursor object using cursor() method sql = "SELECT * FROM %s" cursor.execute(sql, table) results = cursor.fetchall() Daniel Roseman You can't use a parameter for the table name

Python MySQLdb: connection.close() VS. cursor.close()

眉间皱痕 提交于 2019-11-27 05:24:00
问题 If I use MySQLdb to connect to MySQL-Server through Python. I create a connection and a cursor like this: connection = MySQLdb.connect(...) cursor = connection.cursor() # process When the MySQL-processing is done one should close the connection . Now I was wondering: Is it sufficient to close the connection by doing: connection.close() or do I have to close the cursor first and then the connection ? Like this: cursor.close() connection.close() 回答1: Use with , this tool allows you to create a

cursor.fetchall() vs list(cursor) in Python

冷暖自知 提交于 2019-11-27 03:44:32
Both methods return a list of the returned items of the query, did I miss something here? Or they have identical usages indeed? Any differences performance-wise? If you are using the default cursor, a MySQLdb.cursors.Cursor , the entire result set will be stored on the client side (i.e. in a Python list) by the time the cursor.execute() is completed. Therefore, even if you use for row in cursor: you will not be getting any reduction in memory footprint. The entire result set has already been stored in a list (See self._rows in MySQLdb/cursors.py). However, if you use an SSCursor or

cc1: error: unrecognized command line option “-Wno-null-conversion” within installing python-mysql on mac 10.7.5

心已入冬 提交于 2019-11-27 01:11:23
问题 This error broke my python-mysql installation on Mac 10.7.5. Here are the steps The installed python is 2.7.1, mysql is 64 bit for 5.6.11. The being installed python-mysql is 1.2.4, also tried 1.2.3 Configurations for the installation 1) sudo ln -s /usr/local/mysql/lib /usr/local/mysql/lib/mysql 2) Edit the setup_posix.py and change the following mysql_config.path = "mysql_config" to mysql_config.path = "/usr/local/mysql/bin/mysql_config" 3) sudo python setup.py build Here is the stacktrace

Python - SSH Tunnel Setup and MySQL DB Access

拥有回忆 提交于 2019-11-26 23:38:19
问题 I am trying to connect to my server from my local(windows) and access the MySQL DB With the below code setting up the SSH tunnel through putty, I am not able to access the MySQL DB. con = None con = mdb.connect(user='user',passwd='password',db='database',host='127.0.0.1',port=3308) cur = con.cursor() With the below code, I am using paramiko to setup SSH tunnel which is successful but I am not able to connect to MySQL DB ssh = paramiko.SSHClient() ssh.set_missing_host_key_policy(paramiko

Python MYSQL update statement

99封情书 提交于 2019-11-26 21:56:28
I'm trying to get this Python MYSQL update statement correct(With Variables): cursor.execute ("UPDATE tblTableName SET Year=%s" % Year ", Month=%s" % Month ", Day=%s" % Day ", Hour=%s" % Hour ", Minute=%s" Minute "WHERE Server=%s " % ServerID) Any ideas where I'm going wrong? It should be : cursor.execute (""" UPDATE tblTableName SET Year=%s, Month=%s, Day=%s, Hour=%s, Minute=%s WHERE Server=%s """, (Year, Month, Day, Hour, Minute, ServerID)) You can also do it with basic string manipulation, cursor.execute ("UPDATE tblTableName SET Year=%s, Month=%s, Day=%s, Hour=%s, Minute=%s WHERE Server='