mysql+python 简单案例

1、服务启动和停止

启动:net start mysql

停止:net stop mysql

重启:mysql -u root -p

密码:123456

2、简单创建数据库、创建表、插入数据、查询数据

 

import pymysql

conn = pymysql.connect(host='127.0.0.1', user='root', passwd='123456', db='mysql')

cursor = conn.cursor()
cursor.execute('CREATE DATABASE TESTING')
cursor.execute('USE TESTING')
cursor.execute('CREATE TABLE pages (id BIGINT(7) NOT NULL AUTO_INCREMENT, '
               'title VARCHAR(200),content VARCHAR(10000), '
               'created TIMESTAMP DEFAULT CURRENT_TIMESTAMP,'
               ' PRIMARY KEY(id));')
cursor.execute('INSERT INTO pages (title, content) VALUES ("Test page title", "This is some test page content. It can be up to 10,000 characters long.");')
cursor.execute("SELECT * FROM pages WHERE id=1")
print(cursor.fetchone())
cursor.close()
conn.close()

 

 

posted @ 2021-04-22 13:29  SpriteGirl  阅读(131)  评论(0)    收藏  举报