Get a small task and plan to finish it within 10 days, documenting every problem and solution you have encountered.
Requirements: Database 1 records in the table name is based on the primary key to hash (4) obtained, transfer it to database 2, database 2 table name is the primary key to hash (5) obtained.
Difficulty Analysis:
1. Interface implementation
2. Import parameters
3, the use and calculation of hash
4, the transfer and operation of records;
Day1
Install the Mysql-python .
Python Operation MySQL Basics
#coding =utf-8import mysqldbconn= mysqldb.connect (host= ' 192.168.1.90 ', port = 3306, user= ' Qsuserwrite ', passwd= ' qs123456 ', db = ' Shiji ', ' cur = conn.cursor () #创建数据表 #cur.execute ("CREATE table student (ID in T, name varchar, class varchar (2), age varchar (TEN)) #插入一条数据 #cur.execute ("INSERT into student values (' + ', ' + ', ' Tom ', ' 3 Year 2 class ', ' 9 ') ") #cur. Execute (" CREATE TABLE student (ID int,name varchar (), class varchar (a), age int) ") # Cur.execute ("Alter tudent add primary key (ID)") sqlinsert= "insert into student values (%s,%s,%s,%s)" Cur.executemany ( sqlinsert,[(' 8 ', ' John ', ' 1 Year 2 class ', ' Ten '), (' 9 ', ' John ', ' 1 Year 2 class ', ' 10 '), (' 10 ') , ' John ', ' 1 Year 2 class ', ' Ten '), (' One ', ' John ', ' 1 Year 2 class ', ' ten '),]) #修改查询条件的数据 #cur.execute ("Updat E Student set class= ' 3 Year 1 class ' WHERE name = ' Tom ' ") #删除查询条件的数据 #cur.execute (" Delete from student where age= ' 9 ' ") cur.cl OSE () Conn.commit () Conn.close ()
1, Python operation is first to establish a Conn connection;
2, cur = conn.cursor ()
Create a cursor by getting to the cursor () method under the database connection conn.
3, Cur.execute ("CREATE TABLE student (ID int, name varchar (), class varchar (), age varchar (10)")
The Execute () method through the cursor cur operation can write to a pure SQL statement. Manipulate the data by writing it in the Execute () method, such as an SQL statement.
4, you can set sqlinsert= "insert into student values (%s,%s,%s,%s)", thereby saving the amount of code;
5. When inserting more than one record,
Cur.executemany (sqlinsert,[ (' 8 ', ' John ', ' 1 Year 2 class ', ' Ten '), (' 9 ', ' John ', ' 1 Year 2 class ', ' Ten '), (' 10 ', ' John ', ' 1 Year 2 class ', ' Ten '), (' One ', ' John ', ' 1 Year 2 class ', ' Ten '), ])
Methods with Executemany, note that after sqlinsert to add the brackets "", each record to be separated by commas;
Record adjustment records about the database. Python implementation