Import pymysql#s LINK Database conn = pymysql.connect ( host = ' 127.0.0.1 ', #被连接数据库的ip地址 port = 3306, #数据库服务端端口号 user = ' root ', #用户名 password = ' 123456 ', #密码 database = ' db1 ', #选择库 charset = ' UTF8 ' # encoding format) #拿到执行sql语句的游标cur = Conn.cursor () #查询语句select_sql = ' select * from Auth ' #执行sql语句cur. Execute (select_sql) #获取单条查询结果 # Res1 = Cur.fetchone () #获取指定行数的查询结果 # res2 = Cur.fetchmany (3) #获取全部查询结果res3 = Cur.fetchall () #输出查询结果 # Note that if there are multiple statements that get the query, The cursor starts the query from the last position # for example, if there are two Cur.fetchall () then the second query result is empty print (RES3) #插入语句 # You can write the inserted value directly, or you can use%s to take a position, and then pass the parameter Insert_sql = ' Insert INTO auth (name,age,address) VALUES (%s,%s,%s) ' #传入参数的时候用元组或者列表来装插入值cur. Execute (insert_sql, (' auth1 ', 18, ' Beijing ') #提交数据conn. Commit () #关闭游标cur. Close () #关闭链接conn. Close ()
MySQL's Pymysql module