MySQL's Pymysql module

Source: Internet
Author: User

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950 import pymysql#s链接数据库conn = pymysql.connect(    host = ‘127.0.0.1‘,  #被连接数据库的ip地址    port = 3306,         #数据库服务端端口号    user = ‘root‘,       #用户名    password = ‘123456‘, #密码    database = ‘db1‘,    #选择库    charset = ‘utf8‘#编码格式)#拿到执行sql语句的游标cur = conn.cursor()#查询语句select_sql = ‘select * from auth‘#执行sql语句cur.execute(select_sql)#获取单条查询结果# res1 = cur.fetchone() #获取指定行数的查询结果# res2 = cur.fetchmany(3)#获取全部查询结果res3 = cur.fetchall()#输出查询结果#注意,如果有多条获取查询的语句,游标会从上一次的位置开始查询#比如说如果有两条 cur.fetchall()   那么第二次查询结果为空print(res3)#插入语句#可以直接写插入的值,也可以用%s来占位,然后传参insert_sql = ‘insert into auth (name,age,address) values (%s,%s,%s)‘ #传入参数的时候用元组或者列表来装插入值cur.execute(insert_sql,(‘auth1‘,18,‘北京‘))#提交数据conn.commit()#关闭游标cur.close()#关闭链接conn.close()

MySQL's Pymysql module

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.