# -*-coding:utf-8-*-
import mysql.connector
# 进行mysql数据库连接 root 123456 test
conn=mysql.connector.connect(user="root",password="123456",database="test")
cursor=conn.cursor()
print("connection mysql success")
# 对数据库的操作--创建表
def crete_table():
sql_create="create table student(id int(4) PRIMARY KEY NOT NULL ,uid VARCHAR(14) NOT NULL ,name VARCHAR(20) NOT NULL ,score DOUBLE NOT NULL )"
cursor.execute(sql_create)
print "create student success"
# 删除表
def delete_table():
sql_delete="DROP TABLE student"
cursor.execute(sql_delete)
print "delete sucess"
# 插入数据
def insert():
sql_insert="insert into student(id,uid,name,score) values(4,'207','dyh',100)"
cursor.execute(sql_insert)
# cursor.rowcount
conn.commit()#提交
conn.close()
print "insert success"
# 查找数据库
def sql_select():
sql_select="select * from student"
cursor.execute(sql_select)
reslut=cursor.fetchall()
print reslut
# crete_table()
insert()
# sql_select()
# delete_table()
连接mysql
最新推荐文章于 2024-06-28 12:57:38 发布