Record adjustment records about the database. Python implementation

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.