Home > other >  Python3 used in the db. The session. The add () with db.session.com, MIT () are unable to increase d
Python3 used in the db. The session. The add () with db.session.com, MIT () are unable to increase d

Time:09-24

Db_demo2. Py

# encoding: utf-8

# model need to inherit from 'db. The Modle, then need to be mapped to the attributes in the table, must be written as' the Column'
# the data type of the

# autoincrement represents the growth of the primary key is the

# needs to call 'db. Create_all ()' to model the real creation to database

The from flask import flask
The from flask_sqlalchemy import SQLAlchemy
The import config

App=Flask (__name__)
App. Config. From_object (config)
The db=SQLAlchemy (app)


# article table
# the create table article (
# id int primary_key autoincrement,
# title varchar (100) not null,
# content text not null,
#)

Class Article (the Model) :
__tablename__='article'
Id=db. The Column (db. The Integer, primary_key=True, autoincrement=True)
Title=the db. The Column (the String (100), the nullable=False)
The content=db. The Column (db) Text, nullable=False)


Db. Create_all ()


@ app. The route ('/')
Def hello_world () :
# increase:
Article1=Article (title='aaa', the content='BBB')
The db. The session. The add (article1)
# transaction
MIT ()
db.session.comreturn 'Hello World! '


If __name__=="__main__ ':
App. The run (debug=True)


____________________________________________________________________________________________________________________________________

The config. Py


# encoding: utf-8

# the dialect + driver://username: password @ host: port/database

The DIALECT='mysql'
DRIVER='pymysql'
The USERNAME='root'
PASSWORD='root'
The HOST='127.0.0.1'
PORT='3306'
The DATABASE='db_demo3'

SQLALCHEMY_DATABASE_URI="{} + {} ://{}, {} @ {}, {}/{}? Charset=utf8 ". The format (
The DIALECT, DRIVER, USERNAME, PASSWORD, HOST, PORT, DATABASE)

CodePudding user response:

Code is no problem, run your code does is also recorded
You confirm the website up and running and in the browser to access the http://127.0.0.1:5000/is?
You if there is a record in the myql, how do you confirm?

The main line directly above the hello_world calls the results?
The hello_world ()
If __name__=="__main__ ':
App. The run (debug=True)

CodePudding user response:

As if my package has a problem, I put all the things to download it again, thank you!
  • Related