Home > other >  Python MySql database operation (create library, create a table, to add authorization)
Python MySql database operation (create library, create a table, to add authorization)

Time:09-21

 
The import MySQLdb # loading mysql database module

Conn=MySQLdb. Connect (host='localhost', user='root', passwd='@ @ @ @ @, port=3306) # connect the MYSQL database
Cursor=conn. Cursor (#) to obtain the cursor
Cursor. The execute (" "" the create database if not exists Stock_Database" "") # create database 'Stock_Database
'Conn. Select_db # (' Stock_Database) select the database 'Stock_Database
'# perform create data table statements
Cursor. The execute (" ", "
The create table historydata_day (
The date date is NOT NULL,
The open FLOAT (8, 2) NOT NULL,
High FLOAT (8, 2) NOT NULL,
The close FLOAT (8, 2) NOT NULL,
Low FLOAT (8, 2) NOT NULL,
Volume FLOAT (16, 2) NOT NULL,
Price_change FLOAT (8, 2) NOT NULL,
P_change FLOAT (8, 2) NOT NULL,
Ma5 FLOAT (8, 2) NOT NULL,
Ma10 FLOAT (8, 2) NOT NULL,
Ma20 FLOAT (8, 2) NOT NULL,
V_ma5 FLOAT (8, 2) NOT NULL,
V_ma10 FLOAT (8, 2) NOT NULL,
V_ma20 FLOAT (8, 2) NOT NULL,
Turnover FLOAT (8, 2) NOT NULL
)
"" ")
Cursor. The execute (" "" alter table historydata_day add code varchar (6) the not Null after date" "") # inserted after 'date' field 'code' field
# in the 'date' insert 'id' field, and the primary key, automatically add
Cursor. The execute (" "" alter table historydata_day add id int unsigned not Null auto_increment primary key after date" "")
Cursor. The execute (" "" alter table historydata_day drop date" "") # delete 'code' field
Cursor. The execute (" "" alter table historydata_day add date date NOT NULL after id" "") # inserted after the 'id' field 'date' field

MIT () # conn.com submit database
Cursor. The close () # close the cursor
Conn. Close () # closes the connection

CodePudding user response:

What's the problem? What's the problem?

CodePudding user response:

If is to share, in the title with "sharing:" prefix

CodePudding user response:

Cursor. The execute (" the create database if not exists weather ")

The second time to use the command is what complains?
Warning: (1007, "Can 't the create database' weather '; The database exists ")
  • Related