flask数据库
This commit is contained in:
Generated
+27
@@ -0,0 +1,27 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project version="4">
|
||||||
|
<component name="DataSourceManagerImpl" format="xml" multifile-model="true">
|
||||||
|
<data-source source="LOCAL" name="identifier.sqlite" uuid="9cc94bfb-f631-4abb-893a-21764c39acea">
|
||||||
|
<driver-ref>sqlite.xerial</driver-ref>
|
||||||
|
<synchronize>true</synchronize>
|
||||||
|
<jdbc-driver>org.sqlite.JDBC</jdbc-driver>
|
||||||
|
<jdbc-url>jdbc:sqlite:identifier.sqlite</jdbc-url>
|
||||||
|
<working-dir>$ProjectFileDir$</working-dir>
|
||||||
|
</data-source>
|
||||||
|
<data-source source="LOCAL" name="example" uuid="d54fc825-ecd3-4abe-b9e6-e373aa6ffcc2">
|
||||||
|
<driver-ref>sqlite.xerial</driver-ref>
|
||||||
|
<synchronize>true</synchronize>
|
||||||
|
<jdbc-driver>org.sqlite.JDBC</jdbc-driver>
|
||||||
|
<jdbc-url>jdbc:sqlite:$PROJECT_DIR$/flask框架/instance/example.db</jdbc-url>
|
||||||
|
<working-dir>$ProjectFileDir$</working-dir>
|
||||||
|
<libraries>
|
||||||
|
<library>
|
||||||
|
<url>file://$APPLICATION_CONFIG_DIR$/jdbc-drivers/Xerial SQLiteJDBC/3.45.1/org/xerial/sqlite-jdbc/3.45.1.0/sqlite-jdbc-3.45.1.0.jar</url>
|
||||||
|
</library>
|
||||||
|
<library>
|
||||||
|
<url>file://$APPLICATION_CONFIG_DIR$/jdbc-drivers/Xerial SQLiteJDBC/3.45.1/org/slf4j/slf4j-api/1.7.36/slf4j-api-1.7.36.jar</url>
|
||||||
|
</library>
|
||||||
|
</libraries>
|
||||||
|
</data-source>
|
||||||
|
</component>
|
||||||
|
</project>
|
||||||
Generated
+1
-3
@@ -3,9 +3,7 @@
|
|||||||
<component name="Black">
|
<component name="Black">
|
||||||
<option name="sdkName" value="Python 3.12 (base)" />
|
<option name="sdkName" value="Python 3.12 (base)" />
|
||||||
</component>
|
</component>
|
||||||
<component name="ProjectRootManager" version="2" languageLevel="JDK_22" project-jdk-name="D:\ProgramTools\Anaconda" project-jdk-type="Python SDK">
|
<component name="ProjectRootManager" version="2" project-jdk-name="python" project-jdk-type="Python SDK" />
|
||||||
<output url="file://$PROJECT_DIR$/out" />
|
|
||||||
</component>
|
|
||||||
<component name="PythonCompatibilityInspectionAdvertiser">
|
<component name="PythonCompatibilityInspectionAdvertiser">
|
||||||
<option name="version" value="3" />
|
<option name="version" value="3" />
|
||||||
</component>
|
</component>
|
||||||
|
|||||||
@@ -0,0 +1,47 @@
|
|||||||
|
from flask import Flask
|
||||||
|
from flask_sqlalchemy import SQLAlchemy
|
||||||
|
from flask_migrate import Migrate
|
||||||
|
|
||||||
|
app = Flask(__name__)
|
||||||
|
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///example.db'
|
||||||
|
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
|
||||||
|
db = SQLAlchemy(app)
|
||||||
|
migrate = Migrate(app, db)
|
||||||
|
|
||||||
|
|
||||||
|
class User(db.Model):
|
||||||
|
id = db.Column(db.Integer, primary_key=True)
|
||||||
|
username = db.Column(db.String(80), unique=True, nullable=False)
|
||||||
|
email = db.Column(db.String(120), unique=True, nullable=False)
|
||||||
|
gender = db.Column(db.String(10)) # 新增的性别列,可以为空
|
||||||
|
ew_gender = db.Column(db.String(10)) # 新增的性别列,可以为空
|
||||||
|
|
||||||
|
def __repr__(self):
|
||||||
|
return f'<User {self.username}>'
|
||||||
|
|
||||||
|
|
||||||
|
# 初始化数据库并添加数据
|
||||||
|
def init_db():
|
||||||
|
with app.app_context():
|
||||||
|
# 创建所有表
|
||||||
|
db.create_all()
|
||||||
|
|
||||||
|
# 检查是否已有数据,避免重复添加
|
||||||
|
if not User.query.first():
|
||||||
|
# 添加初始用户数据
|
||||||
|
users = [
|
||||||
|
User(username='john_doe', email='john@example.com'),
|
||||||
|
User(username='jane_smith', email='jane@example.com'),
|
||||||
|
User(username='bob_johnson', email='bob@example.com')
|
||||||
|
]
|
||||||
|
|
||||||
|
db.session.add_all(users)
|
||||||
|
db.session.commit()
|
||||||
|
print("初始数据已添加")
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
init_db()
|
||||||
|
print("数据库已初始化")
|
||||||
|
|
||||||
|
migrate = Migrate(app, db)
|
||||||
+2
-1
@@ -10,9 +10,10 @@
|
|||||||
<sourceFolder url="file://$MODULE_DIR$/python爬虫/scrapy/scrapy04" isTestSource="false" />
|
<sourceFolder url="file://$MODULE_DIR$/python爬虫/scrapy/scrapy04" isTestSource="false" />
|
||||||
<sourceFolder url="file://$MODULE_DIR$/python爬虫/python爬虫练习/wangyizhaopin" isTestSource="false" />
|
<sourceFolder url="file://$MODULE_DIR$/python爬虫/python爬虫练习/wangyizhaopin" isTestSource="false" />
|
||||||
<sourceFolder url="file://$MODULE_DIR$/python爬虫/python爬虫练习/douban" isTestSource="false" />
|
<sourceFolder url="file://$MODULE_DIR$/python爬虫/python爬虫练习/douban" isTestSource="false" />
|
||||||
|
<sourceFolder url="file://$MODULE_DIR$/flask框架" isTestSource="false" />
|
||||||
<excludeFolder url="file://$MODULE_DIR$/python爬虫/python爬虫练习/novel" />
|
<excludeFolder url="file://$MODULE_DIR$/python爬虫/python爬虫练习/novel" />
|
||||||
</content>
|
</content>
|
||||||
<orderEntry type="jdk" jdkName="Python 3.12 (base)" jdkType="Python SDK" />
|
<orderEntry type="jdk" jdkName="python" jdkType="Python SDK" />
|
||||||
<orderEntry type="sourceFolder" forTests="false" />
|
<orderEntry type="sourceFolder" forTests="false" />
|
||||||
</component>
|
</component>
|
||||||
</module>
|
</module>
|
||||||
Reference in New Issue
Block a user