python - MySQL 数据库调用 : not all arguments converted during string formatting

标签 python mysql python-3.x

当我执行下面给出的代码时,我收到错误并非所有参数在字符串格式化期间都已转换:

pro_title = "FSBT"
print "pro_title: " + pro_title
pro_id_query = "SELECT ID FROM projs WHERE pro_title=%s"
cursor.execute(pro_id_query, pro_title)
db.commit()
row = cursor.fetchone()
pro_id = None
if row is not None:
   pro_id = str(row[0])    
print "pro_id: " + pro_id

我还尝试了格式:

pro_id_query = "SELECT ID FROM projs WHERE title={}"
cursor.execute(pro_id_query.format(pro_title))

仅当我在 {} 周围使用 ' 时才有效:

pro_id_query = "SELECT ID FROM projs WHERE title='{}'"
cursor.execute(pro_id_query.format(pro_title))

我不明白为什么 INSERT 查询可以很好地与 %s 配合使用,而 SELECT 查询却不能:

insert_query = "INSERT INTO projs (title, description) VALUES (%s, %s) ON DUPLICATE KEY UPDATE `title`=%s"
cursor.execute(insert_query, (pro_title, pro_description, pro_title))

最佳答案

pro_title = "FSBI" 
pro_id_query = "SELECT * FROM %s"%(pro_title)
cursor = con.cursor() 
cursor.execute(q) 
result_list = result.fetchall()   
result_list[0][0] 
con.commit() 
con.close()

pro_id_query = cursor.fetchone() while row != False:
print ("The ID is : ", row[0])

*编辑

id = input("Id : ")
name = input("Name : ")
cursor = con.cursor()
cursor.execute(""" INSERT INTO names (id, name) VALUES("%s", "%s")"""
          %(id, name))

关于python - MySQL 数据库调用 : not all arguments converted during string formatting,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48608442/

相关文章:

python - 如何在不重复输出的情况下使用 random.randint()? (关闭)

python - 如何从 python 脚本获取 bashrc/bashprofile

Python:正则表达式迭代以匹配列表元素中的单词

mysql - 错误 1054 (42S22) : Unknown column 'i' in 'field list'

php - 通过查询生成器对两列求和

python - 无法使用 wxPython 打开在 folium 中生成的本地 HTML 文件

Python:从 FuzzyWuzzy ExtractOne() 返回 Pandas DataFrame

php - 我无法通过使用 PDO、PHP、SQL、HTML 的查询来更新某些记录

python-3.x - 使用 TensorFlow 在 2D 中绘制梯度下降轨迹时的 Traceback

python - 在没有 Tee 的情况下在 Python 中克隆生成器