介绍
小编给大家分享一下MySQL数据库的操作案例,希望大家阅读完这篇文章后大所收获、下面让我们一起去探讨吧!
<强> 强>
conn =, pymysql.connect(主机=& # 39;127.0.0.1 # 39;,,=3306港,用户=& # 39;school_spt& # 39;,, passwd=& # 39; 123456 & # 39;,, db=& # 39; school_info& # 39;),,, #返回个链接对象以前><强> 强>
cursor =, conn.cursor ()<强> 强>
用户=& # 39;根# 39; pwd=& # 39; 123456 & # 39; sql=& # 39; select *,得到userinfo where 密码=% s 以及用户名=% & # 39;% (pwd,用户)
executsql命令,,args),,,, #参数可以是列,表元组或者字典 , 列表: , 用户=& # 39;根# 39; pwd=& # 39; 123456 & # 39; sql=& # 39; select *,得到userinfo where 密码=% s 以及用户名=% & # 39; cursor.execute (sql, pwd、用户) 元组 用户=& # 39;根# 39; pwd=& # 39; 123456 & # 39; sql=& # 39; select *,得到userinfo where 密码=% s 以及用户名=% & # 39; 用户)cursor.execute (sql (pwd) , 字典 sql=& # 39; select *,得到userinfo where 密码=%(密码)s 以及用户名=%(用户名)& # 39; cursor.execute (sql({& # 39;密码# 39;:pwd, & # 39;用户名# 39;:用户}))<强> 强>
sql=& # 39; select *,得到用户信息# 39; res=cursor.execute (sql),,, #返回受影响的行数 #获取返回的数据 cursor.fetchone(),,,,,, #获取返回的第一行内容 cursor.fetchmany (n),,,, #获取返回的前n行内容 cursor.fetchall(),,,,,,,,,, #获取返回的全部内容 , #返回的数据默认是元组形式,如果要以字典形式显示 cursor =, conn.cursor(光标=pymysql.cursors.DictCursor)<强> 强>
,sql=癷nsert into 用户信息(用户名、密码),值(% s % s) ' ,,,cursor.execute (sql,(& # 39;根# 39;& # 39;123 & # 39;));,,,#单条插入 ,,,也可以使用批量插入数据 ,,,cursor.executemany (sql,((& # 39;根# 39;& # 39;123 & # 39;),(& # 39;root1& # 39; & # 39; 1234 & # 39;), (& # 39; root2& # 39; & # 39; 123 & # 39;)]);
<强> 强>
<强> 强>
cursor.scroll(1模式=& # 39;相对# 39;),,#,相对当前位置移动 cursor.scroll(2模式=& # 39;绝对# 39;),#,相对绝对位置移动<强> 强>
id=游标。lastrowid<强> 强>
cursor.close(),, #先关闭游标 conn.close(),,,, #再关闭连接看完了这篇文章,相信你对MySQL数据库的操作案例有了一定的了解,想了解更多相关知识,欢迎关注行业资讯频道,感谢各位的阅读!
MySQL数据库的操作案例