使用peewee结合sqlite做桌面软件的数据库

发表时间:2020-06-28 04:27:08
摘要信息

使用peewee结合sqlite做桌面软件的数据库

sqlite3是自带的貌似

pip安装下 peewee

链接数据库文件,如果不存在就创建

import sqlite3
import os
from peewee import *
import datetime

if os.path.exists('sql.db') == False:
print('数据库文件不存在')

con = sqlite3.connect("sql.db")

db = SqliteDatabase('sql.db')


定义模型文件

class BaseModel(Model):
class Meta:
database = db


class Group(BaseModel):
group = CharField(null=False, default=0, max_length=400, help_text='分组备注')
create_time = DateTimeField(default=datetime.datetime.now())

class Meta:
order_by = ('group')
db_table = 'group'


class Word(BaseModel):
word = TextField(null=False, default=0, help_text='广告短语')
create_time = DateTimeField(default=datetime.datetime.now())

class Meta:
order_by = ('word')
db_table = 'word'


class Company(BaseModel):
company = CharField(null=False, default=0, max_length=400)
companyLink = CharField(null=False, default=0, max_length=400)
contact = CharField(null=False, default=0, max_length=400)
wawa = CharField(null=False, default=0, max_length=400, help_text='旺旺名')
province = CharField(null=False, default=0, max_length=400)
city = CharField(null=False, default=0, max_length=400)
person = CharField(null=False, default=0, max_length=400)
tel = CharField(null=False, default=0, max_length=400)
phone = CharField(null=False, default=0, max_length=400)
group = CharField(null=False, default=0, max_length=400, help_text='分组备注')
message = IntegerField(default=1)
create_time = DateTimeField(default=datetime.datetime.now())
last_msg_time = IntegerField(default=0)

class Meta:
order_by = ('company')
db_table = 'company'


class CompanyPrd(BaseModel):
company = CharField(null=False, default=0, max_length=400, help_text='公司')
province = CharField(null=False, default=0, max_length=400)
city = CharField(null=False, default=0, max_length=400)
companyLink = CharField(null=False, default=0, max_length=400, help_text='公司链接')
price = CharField(null=False, default=0, max_length=400, help_text='商品价格')
deal = CharField(null=False, default=0, max_length=400, help_text='成交额')
repurchase = CharField(null=False, default=0, max_length=400, help_text='复购率')
title = CharField(null=False, default=0, max_length=400, help_text='商品标题')
keyword = CharField(null=False, default=0, max_length=400, help_text='商品关键词')
prdLink = CharField(null=False, default=0, max_length=400, help_text='商品链接')
contact = CharField(null=False, default=0, max_length=400, help_text='旺旺链接')
wawa = CharField(null=False, default=0, max_length=400, help_text='旺旺名')
group = CharField(null=False, default=0, max_length=400, help_text='分组备注')
message = IntegerField(default=1)
create_time = DateTimeField(default=datetime.datetime.now())
last_msg_time = IntegerField(default=0)

class Meta:
order_by = ('company')
db_table = 'company_prd'

然后创建数据表

db.create_tables([Company, CompanyPrd, Group, Word])


40% Complete (success)