Python Mysql 简明教程
PHP & MySQL - Insert Records Example
Python 使用 c.execute(q) 功能在一个表格中插入记录,其中 c 是游标,q 是要执行的插入查询。
Syntax
# execute SQL query using execute() method.
cursor.execute(sql)
# commit the record
db.commit()
# get the row id for inserted record
print("ID:", cursor.lastrowid)
# print the number of records inserted
print(mycursor.rowcount, "records inserted.")
Sr.No. |
Parameter & Description |
1 |
$sql 必填 - 用来在一个表格中插入记录的 SQL 查询。 |
Example
尝试以下示例来在一个表格中插入记录 −
复制并粘贴以下示例作为 mysql_example.ty -
#!/usr/bin/python
import MySQLdb
# Open database connection
db = MySQLdb.connect("localhost","root","root@123", "TUTORIALS")
# prepare a cursor object using cursor() method
cursor = db.cursor()
sql = """INSERT INTO tutorials_tbl
(tutorial_title,tutorial_author, submission_date)
VALUES ('HTML 5', 'Robert', '2010-02-10'),
('Java', 'Julie', '2020-12-10'),
('JQuery', 'Julie', '2020-05-10')
"""
# execute SQL query using execute() method.
cursor.execute(sql)
# commit the record
db.commit()
# get the row id for inserted record
print("ID:", cursor.lastrowid)
# print the number of records inserted
print(cursor.rowcount, "records inserted.")
# disconnect from server
db.close()