使用 mysqldb 在 python 中构建 class
Building a class in python with mysqldb
我正在 python 中创建数据库 class 以帮助管理我的 mysql 数据库。
下面的代码一直有效到最后一行
import MySQLdb
class Database:
...
def insert(self, query):
try:
self.cursor.execute(query)
self.connection.commit()
except:
self.connection.rollback()
...
db = Database()
db.insert('INSERT INTO items (title) VALUES ("tester2");')
mystring = "TEST"
db.insert('INSERT INTO items (title) VALUES (%s);', mystring)
最后一行导致错误:
db.insert('INSERT INTO items (title) VALUES (%s);', mystring)
TypeError: insert() takes exactly 2 arguments (3 given)
我试图重新排列 db.insert 括号中的内容,但我无法理解语法。
例如我也试过这个,但是它既没有插入也没有给出错误:
db.insert('''"INSERT INTO items (title) VALUES (%s);", mystring''')
我的问题是我能否获得正确的语法以便使用 %s 插入,或者我是否必须更改 class 的插入函数?
编辑 1
我试过建议的线路:
db.insert("INSERT INTO items (title) VALUES (%s);"% mystring)
和
db.insert('INSERT INTO items (title) VALUES {value};'.format(value=mystring))
既没有给出错误也没有向数据库输入任何值
编辑 2
调用数据库的 main 的完整代码 class:
if name == "main":
db = Database()
mystring = 'Tester1'
mystring2 = 'Tester2'
db.insert('INSERT INTO items (title) VALUES ("Tester3");') #works
db.insert("INSERT INTO items (title) VALUES (%s);" % mystring) #does not work
db.insert('INSERT INTO items (title) VALUES {value};'.format(value=mystring2)) #does not work
print db.query('SELECT * FROM items;')
试试这个:
db.insert("INSERT INTO items (title) VALUES (%s);" % mystring)
基本上,您希望 mystring
成为查询字符串的一部分,而不是单独的参数。
编辑:
您遇到的问题不仅仅是传递一个参数。但也错误地调用了 execute()
:
insert_stat ="INSERT INTO employees (emp_no, first_name, last_name, hire_date) VALUES (%s, %s, %s, %s)"
data = (2, 'Jane', 'Doe', datetime.date(2012, 3, 23))
# execute() takes 2 arguments, first is the statement as above, second argument is a tuple which contains all values
cursor.execute(insert_stmt, data)
在你的情况下,它应该是这样的:
self.cursor.execute('INSERT INTO items (title) VALUES (%s);', (mystring,))
原答案:
您只需传递一个参数query
。因此,您可以将查询格式设置为一个字符串,如下所示:
'INSERT INTO items (title) VALUES {value};'.format(value=mystring)
改变
db.insert('INSERT INTO items (title) VALUES (%s);', mystring)
到
db.insert('INSERT INTO items (title) VALUES {value};'.format(value=mystring))
我正在 python 中创建数据库 class 以帮助管理我的 mysql 数据库。
下面的代码一直有效到最后一行
import MySQLdb
class Database:
...
def insert(self, query):
try:
self.cursor.execute(query)
self.connection.commit()
except:
self.connection.rollback()
...
db = Database()
db.insert('INSERT INTO items (title) VALUES ("tester2");')
mystring = "TEST"
db.insert('INSERT INTO items (title) VALUES (%s);', mystring)
最后一行导致错误:
db.insert('INSERT INTO items (title) VALUES (%s);', mystring)
TypeError: insert() takes exactly 2 arguments (3 given)
我试图重新排列 db.insert 括号中的内容,但我无法理解语法。
例如我也试过这个,但是它既没有插入也没有给出错误:
db.insert('''"INSERT INTO items (title) VALUES (%s);", mystring''')
我的问题是我能否获得正确的语法以便使用 %s 插入,或者我是否必须更改 class 的插入函数?
编辑 1
我试过建议的线路:
db.insert("INSERT INTO items (title) VALUES (%s);"% mystring)
和
db.insert('INSERT INTO items (title) VALUES {value};'.format(value=mystring))
既没有给出错误也没有向数据库输入任何值
编辑 2
调用数据库的 main 的完整代码 class:
if name == "main":
db = Database()
mystring = 'Tester1'
mystring2 = 'Tester2'
db.insert('INSERT INTO items (title) VALUES ("Tester3");') #works
db.insert("INSERT INTO items (title) VALUES (%s);" % mystring) #does not work
db.insert('INSERT INTO items (title) VALUES {value};'.format(value=mystring2)) #does not work
print db.query('SELECT * FROM items;')
试试这个:
db.insert("INSERT INTO items (title) VALUES (%s);" % mystring)
基本上,您希望 mystring
成为查询字符串的一部分,而不是单独的参数。
编辑:
您遇到的问题不仅仅是传递一个参数。但也错误地调用了 execute()
:
insert_stat ="INSERT INTO employees (emp_no, first_name, last_name, hire_date) VALUES (%s, %s, %s, %s)"
data = (2, 'Jane', 'Doe', datetime.date(2012, 3, 23))
# execute() takes 2 arguments, first is the statement as above, second argument is a tuple which contains all values
cursor.execute(insert_stmt, data)
在你的情况下,它应该是这样的:
self.cursor.execute('INSERT INTO items (title) VALUES (%s);', (mystring,))
原答案:
您只需传递一个参数query
。因此,您可以将查询格式设置为一个字符串,如下所示:
'INSERT INTO items (title) VALUES {value};'.format(value=mystring)
改变
db.insert('INSERT INTO items (title) VALUES (%s);', mystring)
到
db.insert('INSERT INTO items (title) VALUES {value};'.format(value=mystring))