Home > Database > Mysql Tutorial > Detailed explanation of how Python establishes a database connection and inserts data

Detailed explanation of how Python establishes a database connection and inserts data

零下一度
Release: 2017-05-10 15:57:52
Original
2971 people have browsed it

This article mainly explains in detailPythonHow to establish a database connection andInsert data. It has certain reference value. Interested friends can refer to it

Establish database connection

def create_db_connect():  
"""  brief info for: create_db_connect         
建立数据库链接          
Args:          Return:          Raise:    
"""    conn = MySQLdb.connect(host = "rm-uf6wz3f7kb8sx983zo.mysql.rds.aliyuncs.com",                             
user = "pv_cms",                             
passwd = "pv_cms@123",                             
port = 3306,                             
charset = "utf8",                             
db = "pv_interaction_bigdata")    
      return conn
Copy after login

Insert data:

def insert_to_info(conn):    
cursor = conn.cursor()    
sql = '''insert into info values(%s,%s)'''    
l = [['liza','mary'],['dh','lxy']]#必须是list    
cursor.executemany(sql,l)#执行多条插入数据操作    
conn.commit()# 不执行不能插入数据    
conn.close()
def insert_into_info(conn):    
cursor = conn.cursor()    s
ql = '''insert into info values(%s,%s)'''    
l = ('lisa','mary')#必须是tuple    
cursor.execute(sql,l)#插入数据操作    
conn.commit()# 不执行不能插入数据    
conn.close()
Copy after login

[Related recommendations]

1. Free mysql online video tutorial

2. MySQL latest manual tutorial

3. Boolean Education Yan Shiba mysql introductory video tutorial

The above is the detailed content of Detailed explanation of how Python establishes a database connection and inserts data. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template