Does anyone know if there's a way to automatically expand a list in Python, separated by commas? I'm writing some Python code that uses the MySQLdb library, and I'm trying to dynamically update a list of rows in a MySQL database with certain key values.

有没有人知道是否有一种方法可以自动扩展Python中的列表,用逗号分隔?我正在编写一些使用MySQLdb库的Python代码,我正在尝试使用某些键值动态更新MySQL数据库中的行列表。

For instance, in the code below, I'd like to have the numeric values in the record_ids list expand into a SQL "IN" clause.

例如,在下面的代码中,我想让record_ids列表中的数值扩展为SQL“IN”子句。

import MySQLdb
record_ids = [ 23, 43, 71, 102, 121, 241 ]

mysql = MySQLdb.connect(user="username", passwd="secret", db="apps")
mysql_cursor = mysql.cursor()

sqlStmt="UPDATE apps.sometable SET lastmod=SYSDATE() where rec_id in ( %s )"

mysql_cursor.execute( sqlStmt, record_ids )
mysql.commit()

Any help would be appreciated!

任何帮助,将不胜感激!

5 个解决方案

#1


18

try:

",".join( map(str, record_ids) )

",".join( list_of_strings ) joins a list of string by separating them with commas

“,”。join(list_of_strings)通过用逗号分隔它们来连接字符串列表

if you have a list of numbers, map( str, list ) will convert it to a list of strings

如果您有一个数字列表,map(str,list)会将其转换为字符串列表

更多相关文章

  1. 使用PHP和Javascript的组合填充下拉列表
  2. mysql 排序两个字段/列表先根据时间升序排序,时间相同再根据商家I
  3. 从python列表创建MySql表
  4. [实战]MVC5+EF6+MySql企业网盘实战(27)——应用列表
  5. mysql中逗号分隔字段的更好替代方案
  6. 获取项目列表的更好方法:缓存序列化数据与数据库查询或其他?
  7. 如何从php中的数据库表创建表单下拉列表?
  8. MySQL查询返回用户的收件人列表
  9. 迭代angularjs中对象中的属性列表

随机推荐

  1. 给自己的项目做极光推送的步骤
  2. 如何为后台工作创建绑定服务(Xamarin)
  3. Android开发 处理拍照完成后的照片角度
  4. 关于android中sharedpreferences数据不更
  5. android面试题总结
  6. Android开发-直播视讯(3)-创建一个Ubuntu
  7. findViewById()返回布局XML中自定义组件
  8. 基于Android6.0的RIL底层模块分析
  9. 在Webview上加载脱机更新页面
  10. 如何使用java RESTful Web服务将通知推送