Record-the interaction between python and HUAWEI CLOUD database MYSQL

Preface

      In the learning process, I want to save the processed pictures to the cloud for more convenient management and use, so I learned a little about this. (But basically they are all official SDKs.)
      This article mainly records the use of Huawei Cloud . (Because it took too much unnecessary time to find a suitable cloud platform and to deal with network interface problems, I wanted to record it for the convenience of others.) In the
      latter case, I actually found that using a database to store pictures is not efficient enough. , It is best to use cloud storage to store specific content, use a database to store the download path of the data and store the key information of the data. This can achieve more efficient access to pictures and information management.
      If there is any problem with the article, please correct me.

HUAWEI CLOUD

registered

       This is actually nothing to talk about, just register + real-name authentication.

ApsaraDB for RDS

       After our real-name certification, we can receive a free one-month HUAWEI CLOUD database. Insert picture description here
      Because I have already received it, there is no specific configuration diagram. For data that is not very important for storage like me, and is mainly used for learning, the default configuration of security groups and virtual private clouds will do. When we create a database instance, we will enter such a page.Insert picture description here

      First click on the data management service DAS. Insert picture description here
      Then click New Database Login and add another account. After adding the account, we return to the instance management. Click the name of the instance you created. Then click the connection management on the left.Insert picture description here

Here is the point, knock on the blackboard! (It took me a long time here)

Insert picture description here
      There are public network connections and intranet connections. Because we use python for remote interaction, we must choose an external network connection. (At first I thought it was a problem with my interface and security group rules, but I was stupid to change it for a long time... and many online tutorials used the previous Huawei Cloud , which has a remote connection button. I also looked for it. It’s been a long time...) When it Insert picture description here
      comes to the public network, at this time we need to apply for an elastic public network IP. In the network part of Huawei Cloud , there is an elastic public network IP EIP. After entering, we can directly click on the upper right corner to buy flexible public IP. If it is for personal use, just set the configuration as mine. The price is still very cheap. Insert picture description here
      After the purchase is completed, it will provide us with an IP, which is our public network IP, we go back to the public network connection and click bind. After the binding is complete, we go to the security group rules below and click one-click add. (One-click addition is to allow all IP access. Of course, the wrong password is definitely not enough. If you have the ability, you can customize the rules. I tried to specify the address, but I don’t know why I can’t access it, and I didn’t go into it. If you know, you can tell me Bottom) If you only use the Huawei Cloud database, then it is already configured. You can interact with it later through python.

Python connection to the database

      The following is the program on the python side.

  import pymysql
    #连接数据库
    db = pymysql.connect(host = '公网IP' 
    ,user = "用户名" 
    ,passwd=" 密码"
    ,port= 端口号
    ,db="数据库名称" # 数据库名称
    ,charset='utf8'# 字符编码
    )
    db.close() #关闭连接

      The user name and password here are the account and password set when we added the database login above.
      If no error is reported when running this program, it means that you have successfully configured and can connect.

python uses database

      Because I haven't studied database, I just read a few basic sentences, so if I make a mistake, let me know.
      I will introduce two kinds of introductions, one is insertion, and the other is query.
      For the convenience of viewing, I posted the complete program directly.
      If the column name is in Chinese characters, there is no need to put quotation marks outside! The condition behind the query where is not necessarily equal, just define it yourself.
      It is best to look at the basic statement knowledge of sql, simple application is not difficult. Ten minutes is enough.

insert

  import pymysql
    #连接数据库
    db = pymysql.connect(host = '公网IP' 
    ,user = "用户名" 
    ,passwd=" 密码"
    ,port= 端口号
    ,db="数据库名称" # 数据库名称
    ,charset='utf8'# 字符编码
    )`
    # 使用 cursor() 方法创建一个游标对象 cursor
    cursor = db.cursor()
    # 插入命令
    #此处的%s为占位符
    sql_insert="insert into 表的名称(列的名称1,列的名称2,列的名称3) values (%s,%s,%s)"
    #元组的形式传入值   
    data = (存入的数据1,存入的数据2,存入的数据3)
    cursor.execute(sql_insert,data)#执行插入
    #不提交的话云数据库不会改变
    db.commit() #提交到数据库执行
    cursor.close()# 关闭游标
    db.close() #关闭连接

Inquire

  import pymysql
    #连接数据库
    db = pymysql.connect(host = '公网IP' 
    ,user = "用户名" 
    ,passwd=" 密码"
    ,port= 端口号
    ,db="数据库名称" # 数据库名称
    ,charset='utf8'# 字符编码
    )`
    # 使用 cursor() 方法创建一个游标对象 cursor
    cursor = db.cursor()
    # 查询命令
    #此处的%s为占位符
    sql_query='select 想要获取的列 from 表的名称 where 查询已知条件的列名=%s'
    #如果是多个判断,则还是以元组的形式传入值   
    data = 条件
    cursor.execute(sql_query,data)#执行查询
    result = cursor.fetchone()[0]#接受符合条件的一条数据
    #result里面存放的就是查询结果了
    cursor.close()# 关闭游标
    db.close() #关闭连接

Guess you like

Origin blog.csdn.net/NicolasLearner/article/details/109178459