python ---mysql基本操作
来源:互联网 发布:java html转xhtml 编辑:程序博客网 时间:2024/06/04 01:05
python —mysql基本操作
连接数据库
root@kali:~/python# mysql -u rootWelcome to the MySQL monitor. Commands end with ; or \g.Your MySQL connection id is 47Server version: 5.5.41-0+wheezy1 (Debian)Copyright (c) 2000, 2014, Oracle and/or its affiliates. All rights reserved.Oracle is a registered trademark of Oracle Corporation and/or itsaffiliates. Other names may be trademarks of their respectiveowners.Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.mysql> show database;ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'database' at line 1mysql> show databases;+--------------------+| Database |+--------------------+| information_schema || csvt || csvt04 || cvst || mysql || performance_schema |+--------------------+6 rows in set (0.00 sec)mysql> use csvt04Reading table information for completion of table and column namesYou can turn off this feature to get a quicker startup with -ADatabase changedmysql> show tables;+----------------------------+| Tables_in_csvt04 |+----------------------------+| auth_group || auth_group_permissions || auth_message || auth_permission || auth_user || auth_user_groups || auth_user_user_permissions || blog_blog || blog_entry || django_content_type || django_session || django_site |+----------------------------+12 rows in set (0.00 sec)mysql> mysql> mysql> show databases;+--------------------+| Database |+--------------------+| information_schema || csvt || csvt04 || cvst || mysql || performance_schema || python |+--------------------+7 rows in set (0.00 sec)mysql> use python;Reading table information for completion of table and column namesYou can turn off this feature to get a quicker startup with -ADatabase changedmysql> show tables;+------------------+| Tables_in_python |+------------------+| host |+------------------+1 row in set (0.00 sec)mysql> desc host -> ;+--------+-------------+------+-----+---------+-------+| Field | Type | Null | Key | Default | Extra |+--------+-------------+------+-----+---------+-------+| host | varchar(20) | YES | | NULL | || user | varchar(20) | YES | | NULL | || passwd | varchar(20) | YES | | NULL | |+--------+-------------+------+-----+---------+-------+3 rows in set (0.00 sec)mysql> desc host;#查看目标表内元素+--------+-------------+------+-----+---------+-------+| Field | Type | Null | Key | Default | Extra |+--------+-------------+------+-----+---------+-------+| host | varchar(20) | YES | | NULL | || user | varchar(20) | YES | | NULL | || passwd | varchar(20) | YES | | NULL | |+--------+-------------+------+-----+---------+-------+3 rows in set (0.01 sec)mysql> select * from host;Empty set (0.00 sec)mysql> select * from host;+----------------+-------+--------+| host | user | passwd |+----------------+-------+--------+| 192.168.72.130 | lilei | 123456 |+----------------+-------+--------+1 row in set (0.00 sec)mysql> mysql> mysql> select * from host;+----------------+-------+--------+| host | user | passwd |+----------------+-------+--------+| 192.168.72.130 | lilei | 123456 || 192.168.72.130 | lilei | 123456 || 192.168.72.130 | lilei | 123456 |+----------------+-------+--------+3 rows in set (0.00 sec)mysql> mysql> mysql> select * from host;+----------------+-------+--------+| host | user | passwd |+----------------+-------+--------+| 192.168.72.130 | lilei | 123456 || 192.168.72.130 | lilei | 123456 || 192.168.72.130 | lilei | 123456 |+----------------+-------+--------+3 rows in set (0.00 sec)#更改单个数据mysql> update host set host='192.168.72.132' where user='lilei' limit 1 -> ;Query OK, 1 row affected (0.04 sec)Rows matched: 1 Changed: 1 Warnings: 0mysql> update host set host='192.168.72.132' where user='lilei' limit 1;Query OK, 0 rows affected (0.00 sec)Rows matched: 1 Changed: 0 Warnings: 0mysql> update host set host='192.168.72.133' where user='lilei' limit 2;Query OK, 2 rows affected (0.00 sec)Rows matched: 2 Changed: 2 Warnings: 0mysql> select * from host;+----------------+-------+--------+| host | user | passwd |+----------------+-------+--------+| 192.168.72.133 | lilei | 123456 || 192.168.72.133 | lilei | 123456 || 192.168.72.130 | lilei | 123456 |+----------------+-------+--------+3 rows in set (0.00 sec)mysql> update host set host='192.168.72.131' where user='lilei' limit 1;Query OK, 1 row affected (0.00 sec)Rows matched: 1 Changed: 1 Warnings: 0mysql> select * from host;+----------------+-------+--------+| host | user | passwd |+----------------+-------+--------+| 192.168.72.131 | lilei | 123456 || 192.168.72.133 | lilei | 123456 || 192.168.72.130 | lilei | 123456 |+----------------+-------+--------+3 rows in set (0.00 sec)mysql>
代码:
root@kali:~/python/mysql# cat mysql2.py#/usr/bin/python# --*-- coding:utf-8 --*--import MySQLdbtry: conn = MySQLdb.connect(host='localhost',user='root',passwd='',port=3306)#未指定数据库 cur = conn.cursor()#创建数据库游标 cur.execute('create database if not exists python')#如果数据库python不存在则新建 conn.select_db('python')#使用数据库python #在数据库创建中host表 cur.execute('create table host(host varchar(20),user varchar(20),passwd varchar(20))') #向test表里面插数据 value =['192.168.72.130','lilei','123456'] cur.execute('insert into host values(%s,%s,%s)',value) conn.commit()#提交所有sql语句 cur.close() conn.close()except MySQLdb.Error,e: print 'MySQLdb Erroe %d:%s' % (e.args[0],e.args[1])root@kali:~/python/mysql# root@kali:~/python/mysql# python mysql2.pymysql2.py:8: Warning: Can't create database 'python'; database exists cur.execute('create database if not exists python')#如果数据库python不存在则新建MySQLdb Erroe 1050:Table 'host' already existsroot@kali:~/python/mysql# vi mysql2.pyroot@kali:~/python/mysql# python mysql2.pyroot@kali:~/python/mysql# python mysql2.pyroot@kali:~/python/mysql#
阅读全文
0 0
- python mysql基本操作
- Python mysql 基本操作
- python ---mysql基本操作
- python---mysql基本操作3
- python中mysql基本操作
- 【python学习.Mysql的基本操作使用】
- python操作mysql数据库基本类封装
- python mysql数据库连接及基本操作
- 数据库之python+MySQL基本操作
- 用Python操纵数据库MySql的基本操作
- [python]Python操作MySQL
- Python 基本类型操作
- Python文件基本操作
- Python基本操作
- python nltk 基本操作
- python基本图像操作
- python基本文件操作
- Python 文件基本操作
- 动态数组与静态数组区别
- linux下配置python环境 django配置项目
- Android中UI组件AlertDialog的各种实现
- Springtest与junit结合测试注解失败原因
- 测试需求分析指南
- python ---mysql基本操作
- Ubuntu16.04下Torch安装&程序运行笔记(Is object localization for free?)
- Java打印乘法口诀
- 集训 before
- linux-5 用户的管理
- 【Android笔记】ubuntu 16.04下android调试工具 Vysor 安装及使用注意
- 基于matlab的DTMF信号的产生和检测(1)
- PAT 1011A+B和C
- 了解运算符-C基础第4课