mysql 的一些基础

# -*- coding:utf-8 -*-
import pymysql
# mysql_python python2 中使用者个包支持python操作mysql

# 1链接数据库
db = pymysql.connect(
    # 链接的数据库的host主机地址:默认本地数据库使用localhost或者127.0.0.1,如果是远程数据库。需要设置为主机ip地址
    host="localhost",
    #链接数据库的用户名
    user="root",
    # 链接数据库的密码
    password="123456",
    # 端口号3306 mysql默认端口号
    #80端口 http协议的默认单开
    # 443端口 https协议的默认端口
    port=3306,
    #链接数据库的名字
    db="student",
    # 插入中文 需要配置这两个参数
    user_unicode=True,
    charset="utf8")
# 2 获取游标
#同sqlit3 sql语句一样
cursor = db.cursor()
        # 1 auto_increment 自增量
create_tabl = "CREATE TABLE IF NOT EXISTS stu(name text, age integer)"
cursor.execute(create_tabl)

# 插入数据
insert_sql = "INSERT INTO stu(name,age)VALUES(0,'张凡',22)"
cursor.execute(insert_sql)

猜你喜欢

转载自my.oschina.net/u/3771014/blog/1647352