Tag: Property val Successful return value from ISIS cat data type TCA
Concept of mysql-connector connector
They are often a Python 包
or a class of Python libraries that have already been written. These libraries provide us with the basic functionality of Python to connect to a database server.
? Since it is a package, we first learn to import this package
#! /usr/bin/env Python3
# Coding:utf-8
?
Connector
Print (' import successful ')
Execute the above code
$ Python3 test_connector.py
Import succeeded
Indicates that we have successfully imported the relevant package or module
Connector syntax
To learn the connector syntax, we can refer to: https://dev.mysql.com/doc/connector-python/en/
Connection
The two-part connection is indispensable:
Connection information: 主机
, 端口
, 用户名
, 密码
and 数据库名
Db_config = {
' localhost ', #主机
3306, #端口
'??? ', #用户
'??? ', #密码
' python ', #连接的数据库
}
Join function: All connectors have functions, calledconnect
Mysql. Connect
Note : This function returns an object that represents the connection, and we can think of this 连接对象
as a client.
Cursor
A cursor is obtained from a particular connection and cnn
has one in it cursor方法
that can return a cursor object.
Cnn. cursor ()
The operational database is done through cursors, and a connection can produce multiple cursors.
cursors, which have one execute方法
, simply pass in the arguments we need to execute SQL语句
as parameters, allowing the 连接器
command to MySQL服务器
execute the statement.
Note : execute
There is no return value
So, where are the galaxies that are being investigated?
In fact, the information that was found is in our cursor.
Because, the data we found are in the form of a row (found in a line 记录
), all in the cursor, when we iterate over the cursor, we actually get the row (record), each time we output our iteration, we are outputting one of them.
#! /usr/bin/env Python3
# Coding:utf-8
?
Mysql. Connect
?
Db_config = {
' localhost ',
3306,
'??? ',
'??? ',
' MySQL ', # equivalent to Automatic use
}
?
Connect (* *db_config)
Cnn. cursor ()
Print (cur. Execute (' SELECT Host, user, authentication_string from user ' )
?
Cur
Print (i)
Execute the above code
[Email protected]:~/testgit/learning_connector$ python3 test_cursor.py
None
(ByteArray (b' localhost '), ByteArray (b' root '), b' *81bcf9486900824fb0a3746219755fc21c50d058 ')
(ByteArray (b' localhost '), ByteArray (b' Mysql.sys '), b' *thisisnotavalidpasswordthatcanbeusedhere ')
(ByteArray (b' localhost '), ByteArray (b' Debian-sys-maint '), b' * 599d0c45e62d3a9d58d74c0c5008688f13738ae5 ')
(ByteArray (b '% '), ByteArray (b' tuple '), b' *6bb4837eb74329105ee4568dda7dc67ed2ca2ad9 ')
The connector automatically helps us to automatically convert data types in Python, and MySQL data types.
Shut down
Use connectors, as with files, be sure to remember to close, order is
- First Guan
- Re-close the connection
#! /usr/bin/env Python3
# Coding:utf-8
?
Mysql. Connect
?
Db_config = {
' localhost ',
3306,
'??? ',
'??? ',
' python ', # equivalent to Automatic use
}
?
Connect (* *db_config)
Cnn. cursor ()
?
‘‘‘
CREATE TABLE ' From_connector ' (
' id ' int PRIMARY KEY auto_increment,
' Name ' varchar (not NULL)
)
‘‘‘
?
‘‘‘
SHOW TABLES
‘‘‘
?
Cur. Execute (sql_1)
Cur. Execute (sql_2)
Cur
Print (i)
Cur. Close ()
Cnn. Close ()
PS: Have better things, hope to be able to communicate with you a lot of learning;
MySQL---Connector (python How to manipulate database media, based on Python language)