Python实现JAVA中的PrepareStatement功能
2013-04-20 12:04
459 查看
# import MySQL module import MySQLdb # get user input name = raw_input("Please enter a name: ") species = raw_input("Please enter a species: ") # connect db = MySQLdb.connect(host="localhost", user="joe", passwd="secret",db="db56a") # create a cursor cursor = db.cursor() # execute SQL statement cursor.execute("INSERT INTO animals (name, species) VALUES (%s, %s)",(name, species)) # import MySQL module import MySQLdb # connect db = MySQLdb.connect(host="localhost", user="joe", passwd="secret",db="db56a") # create a cursor cursor = db.cursor() # execute SQL statement cursor.execute("""INSERT INTO test (field1, field2) VALUES ("val1","val2")""") # get ID of last inserted record print "ID of inserted record is ", int(cursor.insert_id()) # import MySQL module import MySQLdb # connect db = MySQLdb.connect(host="localhost", user="joe", passwd="secret",db="db56a") # create a cursor cursor = db.cursor() # dynamically generate SQL statements from list cursor.executemany("INSERT INTO animals (name, species) VALUES (%s,%s)", [('Rollo', 'Rat'), ('Dudley', 'Dolphin'), ('Mark', 'Marmoset')]) # import MySQL module import MySQLdb # initialize some variables name = "" data = [] # loop and ask for user input while (1): name = raw_input("Please enter a name (EOF to end): ") if name == "EOF": break species = raw_input("Please enter a species: ") # put user input into a tuple tuple = (name, species) # and append to data[] list data.append(tuple) # connect db = MySQLdb.connect(host="localhost", user="joe", passwd="secret",db="db56a") # create a cursor cursor = db.cursor() # dynamically generate SQL statements from data[] list cursor.executemany("INSERT INTO animals (name, species) VALUES (%s,%s)",data)
相关文章推荐
- Java PHP Python实现短信验证码和国际短信群发功能
- Python+MoneyRunner+Java实现计算器功能自动化
- 在python project中实现类似java project里的properties文件功能
- #python+java#同样功能的代码两种语言实现
- 使用Python函数和字典实现Java中的switch功能
- python3.4 实现java反射功能
- 使用JAVA的开源API-JExcelAPI来操作Excel,实现基本的功能
- JAVA中实现随机无重复数字的功能
- windows下python实现截图功能
- 详解Python实现按任意键继续/退出的功能
- JAVA enum实现简单状态机功能
- Java中实现系统托盘功能(代码全贴,附加运行截图)
- Java中基于maven实现zxing二维码功能
- Python实现的微信公众号群发图片与文本消息功能实例详解
- java实现邮箱验证的功能
- java代码实现打印功能!
- java中ftp断点上传功能的实现
- python 实现libvirt管理(简单的几个功能)
- JAVA SPRING MVC 实现一个简单的页面显示功能