我想我快疯了,所以请,谢谢你的帮助。。。你知道吗
我正在用AWS的RDS做实验,我已经建立了一个简单的MySQL数据库。你知道吗
一个表:MyTable:id(int,自动递增)、col1(varchar(64))、col2(int)。你知道吗
我尝试了以下代码,但未能正常工作:
myString = base64.standard_b64encode("myTestString")
myInt = 0
config = {
'user': db_username,
'password': db_password,
'host': db_host,
'database': db_database,
'raise_on_warnings': True,
'charset' :'utf8',
}
cnx = mysql.connector.connect(**config)
cursor1 = cnx.cursor()
SQLa = ("INSERT INTO myTable (col1, col2) VALUES (%s, %s)")
SQLav = (myString, myInt)
cursor1.execute(SQLa, SQLav)
cnx.commit()
cursor2 = cnx.cursor()
SQLq = ("SELECT id, col1, col2 FROM myTable WHERE col1 = '%s'")
SQLv = (myString)
cursor2.execute(SQLq, SQLv)
print cursor2.rowcount
rows = cursor2.fetchall()
for row in rows:
print(row)
cursor1.close()
cursor2.close()
cnx.close()
INSERT工作正常,在MySQL工作台中我看到添加了行。你知道吗
尽管选择总是返回-1!你知道吗
我尝试在选择中不使用“”,但出现错误:
mysql.connector.errors.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 '%s' at line 1
我怎样才能做到这一点?你知道吗
谢谢你!你知道吗
您的查询接受%s并将其包装在“”中,这意味着mysql连接器将其视为字符串文本,而不是要由您的值替换的占位符。 删除%s附近的“”,它应该可以正常工作。你知道吗
变成
另外,SQLv=(mystring)并没有变成元组,它仍然是一个字符串 要使SQLv成为元组,必须添加逗号(不是很直观)
相关问题 更多 >
编程相关推荐