zoukankan      html  css  js  c++  java
  • 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()
  • 相关阅读:
    Linux文件与文件系统的压缩
    Linux命令与文件查找
    js兼容pc和移动端的简单拖拽效果
    图片懒加载插件
    css小特效
    创建对象和方法
    距离2021年春节还剩。。。
    固定尺寸的图片焦点图案例
    数据库操作
    简单sql操作
  • 原文地址:https://www.cnblogs.com/ishell/p/Python.html
Copyright © 2011-2022 走看看