1 xpower@xpower-CW65S:~$ sudo service mysql start 2 [sudo] xpower 的密码:
1 xpower@xpower-CW65S:~$ sudo apt-get install mysql-server # 安装MySQL服务端,核心 程序 2 xpower@xpower-CW65S:~$ sudo apt-get install mysql-client # 安装MySQL客户端
xpower@xpower-CW65S:~$ sudo pip install MySQL-python
mysql -u root -p # 有密码的时候的启动方式 .
mysql -u root # 没有密码的时候的启动方式 .
xpower@xpower-CW65S:~$ mysql -u root -p # 启动数据库 Enter password: mysql> show databases; #查看当前所有的数据库 +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | mysql_xpower | | performance_schema | | sys | +--------------------+ 5 rows in set (0.00 sec) mysql> use mysql_xpower # 链接 mysql_xpower 数据库 Database changed mysql> show tables; # 查看 mysql_xpower下的所有表单 +------------------------+ | Tables_in_mysql_xpower | +------------------------+ | Student_information | | employee | | user | +------------------------+ 3 rows in set (0.00 sec) mysql> DROP TABLE user; # 删除表单 user Query OK, 0 rows affected (0.30 sec) mysql> CREATE TABLE user (name VARCHAR(20),password VARCHAR(20)); #创建一个 user 表单 Query OK, 0 rows affected (0.46 sec) mysql> show tables; # 查看 mysql下的所有表单 +------------------------+ | Tables_in_mysql_xpower | +------------------------+ | Student_information | | employee | | user | +------------------------+ 3 rows in set (0.00 sec) mysql> insert into user values(‘tom‘,‘12345‘); Query OK, 1 row affected (0.13 sec) mysql> insert into user values(‘jack‘,‘23456‘); Query OK, 1 row affected (0.04 sec) mysql> insert into user values(‘lucy‘,‘34567‘); Query OK, 1 row affected (0.12 sec) mysql> SHOW TABLES # 查看表单 -> ; +------------------------+ | Tables_in_mysql_xpower | +------------------------+ | Student_information | | employee | | user | +------------------------+ 3 rows in set (0.00 sec) mysql> SELECT * FROM user; 查看 user表单下所有项 +------+----------+ | name | password | +------+----------+ | tom | 12345 | | jack | 23456 | | lucy | 34567 | +------+----------+ 3 rows in set (0.00 sec) mysql> DELETE FROM user WHERE NAME=‘lucy‘; # 删除用户 lucy Query OK, 0 rows affected (0.00 sec) mysql> insert into user values(‘lucy‘,‘34567‘); Query OK, 1 row affected (0.04 sec) mysql> delete from user where name =‘jack‘; Query OK, 1 row affected (0.05 sec) mysql> update user set password=‘111‘ where name =‘lucy‘; # 修改 lucy的密码 . Query OK, 1 row affected (0.12 sec) Rows matched: 1 Changed: 1 Warnings: 0 mysql> select * from user -> ; +------+----------+ | name | password | +------+----------+ | tom | 12345 | | lucy | 111 | +------+----------+
>>> conn= MySQLdb.connect( ... host=‘localhost‘, ... port = 3306, ... user=‘root‘, ... passwd=‘密码‘, ... db = ‘需要链接的数据库‘, ... )
1 >>> cur = conn.cursor()
>>> cur.execute(‘CREATE TABLE stu_info (name VARCHAR(20),stu_id VARCHAR(30))‘) 0L >>> conn.commit() >>>
>>> cur.executemany("insert into stu_info (name,stu_id) values (%s,%s)",(("jack",18),("lucy",19))) 2L
>>> conn.commit()
>>> cur.execute("insert into stu_info (name,stu_id) values (‘yuanchongyang‘,‘156 31030706‘)") 1L >>> cur.execute("insert into stu_info (name,stu_id) values (‘caiweiwei‘,‘1563103 0717‘) ") 1L >>> conn.commit()
上面尝试了一下 , 看看能不能和 git 一样多个数据 一次 commit . 需要了解一下 数据库的工作流程....
>>> cur.execute("select * from stu_info") 4L >>> stus = cur.fetchall() >>> stus
查看表单内容
1 >>> cur.execute("select * from stu_info") 2 4L 3 >>> stus = cur.fetchall() 4 >>> stus 5 ((‘yuanchongyang‘, ‘15631030706‘), (‘caiweiwei‘, ‘15631030717‘), (‘jack‘, ‘18‘), (‘lucy‘, ‘19‘))
1 >>>conn.close() 2 3 Conn.close()关闭数据库连接
>>> import MySQLdb >>> conn = MySQLdb.connect( ... host=‘localhost‘, ... port = 3306, ... user=‘root‘, ... passwd=‘q.123456‘, ... db = ‘mysql_xpower‘, ... ) >>> cur = conn.cursor() >>> sqli = "insert into stu_info values(%s,%s)" >>> cur.execute(sqli,(‘mengmeng‘,123456789)) 1L >>> cur.close() >>> cur = conn.cursor() >>> cur.execute("select * from stu_info") 5L
>>> stus = cur.fetchall() >>> stus ((‘yuanchongyang‘, ‘15631030706‘), (‘caiweiwei‘, ‘15631030717‘), (‘jack‘, ‘18‘), (‘lucy‘, ‘19‘), (‘mengmeng‘, ‘123456789‘))
>>> cur.scroll(0,‘absolute‘)
原文:http://www.cnblogs.com/A-FM/p/5808461.html