001 Test Data: insert excel database using the connection data python

001 Test Data: insert excel database using the connection data python

Recent data do testing, mainly to do reporting system, you need to insert data into the database validation service logic, this time on how to use python scripts into Oracle and Mysql database;

1) Oracle section

Coding. 8 = UTF-# 
Import OS
os.environ [ 'the NLS_LANG'] = 'SIMPLIFIED CHINESE_CHINA.UTF8' # this is very important, will not write error
Import cx_Oracle
Import PANDAS PD AS
# oracle database connection
conn = cx_Oracle.connect ( "system "," 1234 "," localhost: 1521 / XE ")
CN = conn.cursor ()

# pandas library using the data read excel
filepath = r'D: \ data.xlsx '
data = pd.read_excel (filepath)


Query = "" "INSERT INTO Student values (: SNO,: SNAME,: SAGE,: SSEX)" ""
# excel data acquisition cycle
for I in Range (0, len (data)):
A = data.iloc [I, 0]
B = data.iloc [I,. 1]
C = data.iloc [I, 2]
D = data.iloc [I,. 3]
# Print (A, B, C, D)
value = (STR (A) , str (b), int ( c),str(d))
cn.execute(query, value)
cn.close ()
after the submission can be found in the database #
conn.commit ()
conn.Close ()

2) Mysql section
Coding. 8 = UTF-# 
Import pymysql
Import PANDAS PD AS
# database connection
Conn = pymysql.connect (
Host = 'localhost',
User = 'the root',
the passwd = 'ABC1234',
DB = 'Test'
)
CN = conn.cursor ()

# excel data acquisition cycle using pandas
filepath = r'D: \ data.xlsx '
data = pd.read_excel (filepath)
Query = "" "INSERT INTO infoss values (% S,% S,% S)" ""

I in Range for (0, len (Data)):
A data.iloc = [I, 0]
B = data.iloc [I,. 1]
C = data.iloc [I, 2]
value = (int (A) , str (b), int (c))
cn.Execute (Query, value)
cn.Close ()
conn.commit ()
conn.Close ()





EG: novice test, there is a problem we can conduct more exchanges.

Guess you like

Origin www.cnblogs.com/zjx012/p/11574570.html