Linux Python操作 MySQL 数据库(建表)

1.创建表操作
#!/usr/bin/python
# -*- coding: UTF-8 -*-

import MySQLdb
import getpass
password = getpass.getpass() #不回显
# 打开数据库连接
db = MySQLdb.connect("localhost", "root", password, "ztq", charset='utf8' )

# 使用cursor()方法获取操作游标 
cursor = db.cursor()

# 如果数据表已经存在使用 execute() 方法删除表
cursor.execute("drop table if exists test_table;")

# 创建数据表SQL语句
sql = """CREATE TABLE test_table (
         FIRST_NAME  CHAR(20) NOT NULL,
                 LAST_NAME  CHAR(20),
                 AGE INT,  
                 SEX CHAR(1),
                 INCOME FLOAT )"""

cursor.execute(sql)


# 关闭数据库连接
db.close()

 

你可能感兴趣的:(Linux,python,mysql)