Python mysql构建表错误

时间:2017-09-28 04:59:20

标签: python pymysql

#-*-coding:utf-8-*-

import pymysql

class get_Mysql(object):
    def __init__(self,dbname,company,goods_name):
        self.dbname = dbname
        self.table_name = '{}_{}'.format(company,goods_name)
        self.conn = pymysql.connect(
            host = '127.0.0.1',
            user = 'root',
            password = '123456',
            port = 3306,
            db = self.dbname,
            charset = 'utf8'
        )

        self.cursor = self.conn.cursor()

    def create_table(self):
        create_sql = ''' CREATE TABLE '{tbname}' (
    {id} INT (15) PRIMARY KEY,
    {price} VARCHAR (15) NOT NULL,
    {is_jd} CHAR (30) NOT NULL DEFAULT NULL,
    {shopname} VARCHAR (30) NOT NULL DEFAULT NULL,
    {brand} VARCHAR (20) NOT NULL DEFAULT NULL,
    {years} VARCHAR (10) NOT NULL DEFAULT NULL,
    {months} VARCHAR (10) NOT NULL DEFAULT NULL,
    {weight} VARCHAR (10) NOT NULL DEFAULT NULL,
    {thick} VARCHAR (40) NOT NULL DEFAULT NULL,
    {long} VARCHAR (40) NOT NULL DEFAULT NULL,
    {cpu_brand} VARCHAR (30) NOT NULL DEFAULT NULL,
    {cpu_num} VARCHAR (20) NOT NULL DEFAULT NULL,
    {sim_num} VARCHAR (25) NOT NULL DEFAULT NULL,
    {sim} VARCHAR (20) NOT NULL DEFAULT NULL,
    {rom} VARCHAR (10) NOT NULL DEFAULT NULL,
    {ram} VARCHAR (15) NOT NULL DEFAULT NULL,
    {sizes} VARCHAR (20) NOT NULL DEFAULT NULL, 
    {front_c} VARCHAR (20) NOT NULL DEFAULT NULL,
    {back_c} VARCHAR (20) NOT NULL DEFAULT NULL,
    {battery} VARCHAR (45) NOT NULL DEFAULT NULL,
    {total_com} INT (20) NOT NULL DEFAULT 0,
    {good_com} INT (20) NOT NULL DEFAULT 0,
    {mid_com} INT (20) NOT NULL DEFAULT 0,
    {bad_com} INT (20) NOT NULL DEFAULT 0,
    {good_lv} FLOAT (20),
    {mid_lv} FLOAT (20),
    {bad_lv} FLOAT (20)
    )
    '''
        try:
        self.cursor.execute(create_sql.format(tbname=self.table_name,id='id',price='price',is_jd='is_jd',shopname='shopname',brand='brand',
                                              years='years',months='months',weight='weight',thick='thick',long='long',cpu_brand='cpu_brand',
                                              cpu_num='cpu_num',sim_num='sim_num',sim='sim',rom='rom',ram='ram',sizes='sizes',
                                              front_c='front_c',back_c='back_c',battery='battery',total_com='total_com',good_com='good_com',
                                              mid_com='mid_com',bad_com='bad_com',good_lv='good_lv',mid_lv='mid_lv',bad_lv='bad_lv'))

        except Exception as e:
            self.conn.rollback()
            print('Create table failure, cause:',e)

        else:
            self.conn.commit()
            print('The table is successful and the name is{}'.format(self.table_name))


    def insert(self,data):
        insert_sql = '''INSERT INTO '{tbname}'(id,price,is_jd,shopname,brand,years,months,weight,thick,long,cpu_brand,cpu_num,sim_num,sim,
                                            rom,ram,sizes,front_c,back_c,battery,total_com,good_com.mid_com,bad_com,good_lv,mid_lv,bad_lv)
                    VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)                                  
    '''
        try:



self.cursor.execute(insert_sql.format(self.table_name),data['id'],data['price'],data['is_jd'],data['shopname'],data['brand'],data['years'],data['months'],data['weight'],data['thick'],
                                                            data['long'],data['cpu_brand'],data['cpu_num'],data['sim_num'],data['sim'],data['rom'],data['ram'],data['sizes'],data['front_c'],
                                                            data['back_c'],data['battery'],data['total_com'],data['good_com'],data['mid_com'],data['bad_com'],data['good_lv'],data['mid_lv'],data['bad_lv'])

        except Exception as e:
            self.conn.rollback()
            print("Insert data failure, cause:",e)

        else:
            self.conn.commit()
            print('Insert a data successfully!')

    def close_table(self):
        self.cursor.close()
        self.conn.close()



if __name__ == '__main__':
    data ={'id':13108530411,'price':'2900.00','is_jd':'self-operation','shopname':'Chili mobile phone flagship store',"brand":'Hot pepper','years':'2017','months':'June','weight':'164(Contain the battery)',"thick":"8.9(Note: subject to the product configuration and manufacturing process, the actual size of the body is different. Please refer to the physical object)",
           'long':'145(Note: subject to the product configuration and manufacturing process, the actual size of the body is different. Please refer to the physical object)','cpu_brand':'Snapdragon','cpu_num':'Four nuclear','sim_num':'Double card double for single pass','sim':'Nano SIM','rom':'32GB','ram':'4GB',"sizes":'5.0 inches','front_c':'8 million pixels','back_c':'16 million pixels','battery':'4000mAh (Typical capacity)/3900mAh (Nominal capacity','total_com':1400,
           'good_com':1300,'mid_com':60,'bad_com':40,'good_lv':0.925,'mid_lv':0.043,'bad_lv':0.032}

    my = get_Mysql('e-commerce','JD','phone')
    my.create_table()
    my.insert(data)
    my.close_table()

我没有非常使用markdown,我的代码缩进不是问题,我将代码粘贴到stackoverflow一些缩进问题,请忽略。 我使用pymysql创建数据表时犯了一个错误:

创建表失败,原因:(1064,"您的SQL语法中有错误;请查看与您的MySQL服务器版本对应的手册,以便在'&附近使用正确的语法#39; JD_phone'(\ n id INT(15)PRIMARY KEY,\ n price VARCHAR(15)NOT NUL' at line 1") 插入数据失败,导致:' tbname'

我的SQL语句错了,如何在SQL语句中写下剩余的小数点?

1 个答案:

答案 0 :(得分:1)

哦,我已经解决了它,通过测试每个字段测试,我将long字段更改为longs以成功创建数据表,这应该是内置名称与mysql冲突的原因。

表格成功,名称为JD_phone