python通过MySQLdb模块链接数据库并添加数据完整代码演示,pythonmysqldb,#!/usr/bin/p
文章由Byrx.net分享于2019-03-23 10:03:07
python通过MySQLdb模块链接数据库并添加数据完整代码演示,pythonmysqldb,#!/usr/bin/p
#!/usr/bin/pythonimport MySQLdb# Open database connectiondb = MySQLdb.connect("localhost","testuser","test123","TESTDB" )# prepare a cursor object using cursor() methodcursor = db.cursor()# Prepare SQL query to INSERT a record into the database.sql = """INSERT INTO EMPLOYEE(FIRST_NAME, LAST_NAME, AGE, SEX, INCOME) VALUES ('Mac', 'Mohan', 20, 'M', 2000)"""try: # Execute the SQL command cursor.execute(sql) # Commit your changes in the database db.commit()except: # Rollback in case there is any error db.rollback()# disconnect from serverdb.close()
评论关闭