Test engineers to perform insert data manipulation skills python

Test engineers to perform insert data manipulation skills python

1, common database operations there are usually operating data query, modify, add, delete, and so on. Most can be performed in Navicat, the database client preview and operate more convenient. The actual use of the process, add data will often encounter situations need to add bulk, this time using Navicat is not so convenient.
2, using python insert the data and query data principle is the same. First of all get inserted into the database statement.
3, is obtained by inserting Navicat database statement, a data table to find the destination, right data in the table, below a selected Copy of As -> INSERT Statement
Here Insert Picture Description
. 4, which has been copied in the edit page statement paste Query, performed to see whether create a data success. Note that the table institutions judgment here, and find it difficult to insert the data table fields. Until the insert can be used to modify the database.
Here Insert Picture Description
5, the following edit python code

# -*- coding: utf-8 -*-
import time
import pymysql.cursors

connect = pymysql.Connect(
    host='192.168.00.00',
    port=3306,
    user='root',
    passwd='123456',
    db='sqname',
    charset='utf8'
)
# 获取游标
cursor = connect.cursor()
#根据实际语句填写
sq = "INSERT INTO sqname('key')'VALUES'('')"
r= cursor.execute(sq)
connect.commit()
print(r)

6, insert data through automation, the purpose is to insert the data can be used directly, and in line with my data model, which includes multi-table insert, and other requirements associated with the word unity. And a plurality of data can be inserted. code show as below

import time
import pymysql.cursors
connect = pymysql.Connect(
    host='192.168.00.00',
    port=3306,
    user='root',
    passwd='123456',
    db='sqname',
    charset='utf8'
)
# 获取游标
cursor = connect.cursor()
for i in range(100,140): #创建循环设置数据个数
    studentid = '009'+str(i)
    xuehao = '3040'+str(i)
    loginid = '45c'+str(i)
    username = '101008'+str(i)
    studentname = "张三"
    data = (studentid,studentname,xuehao,) #数据间通过studentid关联
    data1 = (loginid, studentid, username,studentname,)
    sq = "INSERT INTO sqname('key')'VALUES'('%s','%s','%s','%s','8a6f2805b4515ac12058e79e66539be9'," \
          "'S','','S','0','2019-10-15 09:38:27',100,NULL,NULL,NULL)"#根据实际语句填写
    r = cursor.execute(sq % data)
    connect.commit()
    sq1 = "INSERT INTO sqname('key')'VALUES'('')"#根据实际语句填写
    r1 = cursor.execute(sq1 % data1)
    connect.commit()
    print("ID:",i,"sucess student:",r,"sucess login :", r1)

cursor.close()
connect.close()
print("over!!!!!!!!!!!!!!!!!!")
Released two original articles · won praise 0 · Views 16

Guess you like

Origin blog.csdn.net/qq_35959900/article/details/104900098