Installation module:
#pip install ....
MYSQLDB (2.x)
Pymysql (3.x)
Import MYSQLDB as SQL
con = Sql.connect (
host = "localhost", #host host, local hostname or IP, refers to the hosts of the database you are connected to
user = "Root", #user the username of the login database
Passwd= "123456", #passwd password for login database user name
db= "MySQL" #你要操作的数据库的名称
#charset = "Utf-8" #编码
#port = 3306 Port Default 3306
)
#实例化mysql游标
#游标: Is the object used to pass Python commands to MySQL and receive data from MySQL returned to Python
cur = con.cursor ()
#利用游标执行数据库命令
Cur.execute ("SELECT * from Host")
#接收数据库的返回
Cur.fetchall () #接收所有的返回
Cur.fetchone () #接收返回的1条
Cur.fetchmany (number) #接收返回的指定条
#关闭游标
Cur.close ()
#提交对数据库的操作
Con.commit () #提交
#关闭数据库连接
Con.close ()
#在我们的工作当中我们使用数据库难点不在我们对数据库的操作上
#我们使用数据库对业务逻辑进行合理的描述才是关键
From function to demand
Modeling: The use of fields and relationships to properly describe our needs
1, to understand the needs
2, according to the needs of the table to build out
3, according to the data in the database management
#如果说我们只是在操作数据, the first difficulty we may encounter is the concatenation of strings and the collation of data structures
Python operation MySQL