python初始化MySQL数据库模板

很基础,但是经常用到,记录一下,省得每次手打

复制代码
#!/bin/env python
# -*- encoding=utf-8 -*-
import MySQLdb

# Database info
host = '192.168.1.136'
port = 3306
user = 'user'
passwd = 'passwd'
db = 'dbname'
table_name = 'tablename'

# connect the database
conn = MySQLdb.connect(host=host, user=user, passwd=passwd, db=db)

# get the cursor
cursor = conn.cursor()

# Create Database
#cursor.execute("create database %s" % database_name)

# Use database
cursor.execute("use %s" % database_name)

# Create tables
cmd = ''' 
CREATE TABLE %s (
    id VARCHAR(28) NOT NULL DEFAULT '',
    profit_counts int(10) NOT NULL DEFAULT 0,
    max_win int(10) NOT NULL DEFAULT 0,
    max_lose int(10) NOT NULL DEFAULT 0,
    avg_profit int(10) NOT NULL DEFAULT 0,
    win_counts int(10) NOT NULL DEFAULT 0,
    avg_win int(10) NOT NULL DEFAULT 0,
    avg_lose int(10) NOT NULL DEFAULT 0,
    PRIMARY KEY (openid)
) ENGINE=INNODB charset=utf8;
'''
cursor.execute(cmd % table_name)

# close database
cursor.close()
conn.commit()
conn.close()
复制代码

 

posted on   卖火柴的小东东  阅读(1226)  评论(0编辑  收藏  举报

编辑推荐:
· 从问题排查到源码分析:ActiveMQ消费端频繁日志刷屏的秘密
· 一次Java后端服务间歇性响应慢的问题排查记录
· dotnet 源代码生成器分析器入门
· ASP.NET Core 模型验证消息的本地化新姿势
· 对象命名为何需要避免'-er'和'-or'后缀
阅读排行:
· 编程神器Trae:当我用上后,才知道自己的创造力被低估了多少
· 开发的设计和重构,为开发效率服务
· 从零开始开发一个 MCP Server!
· Ai满嘴顺口溜,想考研?浪费我几个小时
· 从问题排查到源码分析:ActiveMQ消费端频繁日志刷屏的秘密

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示