当前位置: 首页 > news >正文

十堰建设局网站威海网站建设在哪

十堰建设局网站,威海网站建设在哪,重庆网站设计定制,wordpress 主题字号一#xff0c;安装mysql如果是windows用户#xff0c;mysql的安装非常简单#xff0c;直接下载安装文件#xff0c;双击安装文件一步一步进行操作即可。Linux 下的安装可能会更加简单#xff0c;除了下载安装包进行安装外#xff0c;一般的linux仓库中都会有mysql#x…一安装mysql如果是windows用户mysql的安装非常简单直接下载安装文件双击安装文件一步一步进行操作即可。Linux 下的安装可能会更加简单除了下载安装包进行安装外一般的linux仓库中都会有mysql我们只需要通过一个命令就可以下载安装Ubuntu\deepinsudo apt-get install mysql-serverSudo apt-get install  mysql-clientcentOS/redhatyum install mysql二安装MySQL-python要想使python可以操作mysql就需要MySQL-python驱动它是python操作mysql必不可少的模块。下载MySQL-python-1.2.5.zip 文件之后直接解压。进入MySQL-python-1.2.5目录:python setup.py install三测试测试非常简单检查MySQLdb模块是否可以正常导入。fnngjfnngj-H24X:~/pyse$ pythonPython 2.7.4 (default, Sep 26 2013, 03:20:56)[GCC 4.7.3] on linux2Type help, copyright, credits or license for more information. import MySQLdb没有报错提示MySQLdb模块找不到说明安装OK下面开始使用python操作数据库之前我们有必要来回顾一下mysql的基本操作四mysql的基本操作$ mysql -u root -p  (有密码时)$ mysql -u root     (无密码时)mysql show databases;  // 查看当前所有的数据库--------------------| Database           |--------------------| information_schema || csvt               || csvt04             || mysql              || performance_schema || test               |--------------------6 rows in set (0.18 sec)mysql use test;   //作用与test数据库Database changedmysql show tables;   //查看test库下面的表Empty set (0.00 sec)//创建user表name 和password 两个字段mysql CREATE  TABLE  user (name VARCHAR(20),password VARCHAR(20));  Query OK, 0 rows affected (0.27 sec)//向user表内插入若干条数据mysql insert into user values(Tom,1321);Query OK, 1 row affected (0.05 sec)mysql insert into user values(Alen,7875);Query OK, 1 row affected (0.08 sec)mysql insert into user values(Jack,7455);Query OK, 1 row affected (0.04 sec)//查看user表的数据mysql select * from user;----------------| name | password |----------------| Tom  | 1321     || Alen | 7875     || Jack | 7455     |----------------3 rows in set (0.01 sec)//删除name 等于Jack的数据mysql delete from user where name  Jack;Query OK, 1 rows affected (0.06 sec)//修改name等于Alen 的password 为 1111mysql update user set password1111 where name  Alen;Query OK, 1 row affected (0.05 sec)Rows matched: 1  Changed: 1  Warnings: 0//查看表内容mysql select * from user;------------------| name   | password |------------------| Tom    | 1321     || Alen   | 1111     |------------------3 rows in set (0.00 sec)五python操作mysql数据库基础#codingutf-8import MySQLdbconn MySQLdb.connect(hostlocalhost,port  3306,userroot,passwd123456,db test,)cur  conn.cursor()#创建数据表#cur.execute(create table student(id int ,name varchar(20),class varchar(30),age varchar(10)))#插入一条数据#cur.execute(insert into student values(2,Tom,3 year 2 class,9))#修改查询条件的数据#cur.execute(update student set class3 year 1 class where name  Tom)#删除查询条件的数据#cur.execute(delete from student where age9)cur.close()conn.commit()conn.close() conn  MySQLdb.connect(hostlocalhost,port  3306,userroot, passwd123456,db test,)Connect() 方法用于创建数据库的连接里面可以指定参数用户名密码主机等信息。这只是连接到了数据库要想操作数据库需要创建游标。 cur  conn.cursor()通过获取到的数据库连接conn下的cursor()方法来创建游标。 cur.execute(create table student(id int ,name varchar(20),class varchar(30),age varchar(10)))通过游标cur操作execute()方法可以写入纯sql语句。通过execute()方法中写如sql语句来对数据进行操作。cur.close()cur.close() 关闭游标conn.commit()conn.commit()方法在提交事物在向数据库插入一条数据时必须要有这个方法否则数据不会被真正的插入。conn.close()Conn.close()关闭数据库连接六插入数据通过上面execute()方法中写入纯的sql语句来插入数据并不方便。如cur.execute(insert into student values(2,Tom,3 year 2 class,9))我要想插入新的数据必须要对这条语句中的值做修改。我们可以做如下修改#codingutf-8import MySQLdbconn MySQLdb.connect(hostlocalhost,port  3306,userroot,passwd123456,db test,)cur  conn.cursor()#插入一条数据sqliinsert into student values(%s,%s,%s,%s)cur.execute(sqli,(3,Huhu,2 year 1 class,7))cur.close()conn.commit()conn.close()假如要一次向数据表中插入多条值呢#codingutf-8import MySQLdbconn MySQLdb.connect(hostlocalhost,port  3306,userroot,passwd123456,db test,)cur  conn.cursor()#一次插入多条记录sqliinsert into student values(%s,%s,%s,%s)cur.executemany(sqli,[(3,Tom,1 year 1 class,6),(3,Jack,2 year 1 class,7),(3,Yaheng,2 year 2 class,7),])cur.close()conn.commit()conn.close()executemany()方法可以一次插入多条值执行单挑sql语句,但是重复执行参数列表里的参数,返回值为受影响的行数。七查询数据也许你已经尝试了在python中通过cur.execute(select * from student)来查询数据表中的数据但它并没有把表中的数据打印出来有些失望。来看看这条语句获得的是什么aacur.execute(select * from student)print aa5它获得的只是我们的表中有多少条数据。那怎样才能获得表中的数据呢进入python shell import MySQLdb conn  MySQLdb.connect(hostlocalhost,port  3306,userroot,    passwd123456,db test,) cur  conn.cursor() cur.execute(select * from student)5L cur.fetchone()(1L, Alen, 1 year 2 class, 6) cur.fetchone()(3L, Huhu, 2 year 1 class, 7) cur.fetchone()(3L, Tom, 1 year 1 class, 6)...cur.scroll(0,absolute)fetchone()方法可以帮助我们获得表中的数据可是每次执行cur.fetchone() 获得的数据都不一样换句话说我没执行一次游标会从表中的第一条数据移动到下一条数据的位置所以我再次执行的时候得到的是第二条数据。scroll(0,absolute) 方法可以将游标定位到表中的第一条数据。还是没解决我们想要的结果如何获得表中的多条数据并打印出来呢#codingutf-8import MySQLdbconn MySQLdb.connect(hostlocalhost,port  3306,userroot,passwd123456,db test,)cur  conn.cursor()#获得表中有多少条数据aacur.execute(select * from student)print aa#打印表中的多少数据info  cur.fetchmany(aa)for ii in info:    print iicur.close()conn.commit()conn.close()通过之前的print aa我们知道当前的表中有5条数据fetchmany()方法可以获得多条数据但需要指定数据的条数通过一个for循环就可以把多条数据打印出啦执行结果如下5(1L, Alen, 1 year 2 class, 6)(3L, Huhu, 2 year 1 class, 7)(3L, Tom, 1 year 1 class, 6)(3L, Jack, 2 year 1 class, 7)(3L, Yaheng, 2 year 2 class, 7)[Finished in 0.1s]
http://www.zqtcl.cn/news/816623/

相关文章:

  • 怎样更新网站泉州网站开发公司
  • 蕲春县住房和城乡建设局网站广东建设局网站首页
  • 网站优化工作室共享经济型网站开发
  • 自己做网站好还是购买网站好网站建设平台报价
  • 设计师配色网站太原建站模板源码
  • 学计算机的做网站的叫什么工作wordpress商用收费不
  • 青岛网站建设谁家好一些网页微信怎么登陆
  • 企业网站seo优做网站的旅行社
  • 十大免费自助建站上传网站到空间
  • 深圳企业做网站简约个人网站
  • 茂名放心营销网站开发网站怎么做app
  • php语言 网站建设专业的外贸网站建设公司价格
  • 看英语做游戏的网站wordpress与微信对接
  • 企业网站打不开了看守所加强自身网站建设工作
  • 长汀网站建设做电池的有哪些网站
  • 做软件常用的网站厦门建设局地址
  • 沭阳三剑客做网站科技 公司 响应式 网站
  • 深圳网站建设培训哪家好曲阜网架公司
  • wordpress建立网站实例贵阳网站开发谁家做的好
  • 百度网站推广怎么收费中国科技成果
  • 枣庄企业网站建设wordpress 评论群发
  • 网站视觉设计方案视频制作素材
  • 哪个网站专做民宿wordpress 主题教程
  • 网站后台 设计北京海淀区官网
  • 公司官网网站建设想法wordpress oss
  • 如何自己创建网站招聘网站代理
  • 手机网页视频提取工具seo网站是什么
  • seo网站优化公司龙岩网站设计一般要多久
  • 江苏自助建站系统哪家好go语言网站开发
  • 建设网站 注册与登陆wordpress产品上传