MySQL is the most widely used database server world Web, SQLite particular is a lightweight, embeddable, but can not afford the high concurrent access for desktop and mobile applications. And MySQL is a server-side database designed to withstand high concurrent access, while the amount of memory is far greater than SQLite. In addition, MySQL has a variety of internal database engine, the most commonly used InnoDB database engine that supports transactions.
First, install MySQL
Not presented here in detail, and if there is not installed can refer to this blog: http://www.cnblogs.com/wj-1314/p/7573242.html
Second, install MySQL-python
To make python may need to operate mysql MySQL-python drive, it is essential python mysql module operation.
Download: https://pypi.python.org/pypi/MySQL-python/
After unzipping the downloaded files directly MySQL-python-1.2.5.zip. Into the MySQL-python-1.2.5 directory:
>>python setup.py install
Then install pymysql
pip install pymysql
Third, the test module pymysql
Very simple test to check if pymysql module can be imported properly. (At the time of operation of the database, python2 general use mysqldb, but is no longer supported in python3 in mysqldb, we can use pymysql and mysql.connector. All paper operations are done under the pymysql python3.)
No error prompt MySQLdb module can not be found, the installation OK
Four, MySQL basic operations
mysql> show databases; // View all current database + -------------------- + | Database | +--------------------+ | information_schema | | csvt | | csvt04 | | mysql | | performance_schema | | test | +--------------------+ rows in set (0.18 sec) MySQL > use test; // function and test database Database changed MySQL > Show the Tables; // Check test library table below Empty set (0.00 sec) // Create a user table, name and password fields two mysql> CREATE TABLE user (name VARCHAR(20),password VARCHAR(20)); Query OK, 0 rows affected (0.27 sec) // inserting several pieces of data into the user table mysql> insert into user values('Tom','1321'); Query OK, 1 row affected (0.05 sec) mysql> insert into user values('Alen','7875'); Query OK, 1 row affected (0.08 sec) mysql> insert into user values('Jack','7455'); Query OK, 1 row affected (0.04 sec) // Check user data table mysql> select * from user; +------+----------+ | name | password | +------+----------+ | Tom | 1321 | | Alen | 7875 | | Jack | 7455 | +------+----------+ rows in set (0.01 sec) // delete the name Jack equal data mysql> delete from user where name = 'Jack'; Query OK, 1 rows affected (0.06 sec) // change the name of the password is equal to Alen 1111 MySQL > SET Update User password = ' 1111 ' WHERE name = ' Alen ' ; Query OK, 1 row affected (0.05 sec) Rows matched: 1 Changed: 1 Warnings: 0 // Check the table of contents mysql> select * from user; +--------+----------+ | name | password | +--------+----------+ | Tom | 1321 | | Alen | 1111 | +--------+----------+ rows in set (0.00 sec)
Five, Python operate mysql database infrastructure
Python flow of operation shown here MySQL database in flowchart:
1, the implementation of SQL, the specific statement is as follows:
#coding=utf-8 import MySQLdb # Open Database Connectivity
conn= MySQLdb.connect( host='localhost', port = 3306, user='root', passwd='123456', db ='test', ) Using # cursor () method to get the cursor operation cur = conn.cursor() # Create a data table #cur.execute("create table student(id int ,name varchar(20),class varchar(30),age varchar(10))") # Insert a data #cur.execute("insert into student values('2','Tom','3 year 2 class','9')") # Modify the data query condition #cur.execute("update student set class='3 year 1 class' where name = 'Tom'") # Delete query data #cur.execute("delete from student where age='9'") # Close the cursor cur.close () # submit, or can not save new or modified data conn.commit () # close the connection conn.close()
2, access to the newly created data increment ID
import pymysql conn = pymysql.connect(host='127.0.0.1', port=3306, user='root', passwd='123', db='t1') cursor = conn.cursor() cursor.executemany("insert into hosts(host,color_id)values(%s,%s)", [("1.1.1.11",1),("1.1.1.11",2)]) conn.commit() cursor.close() conn.close() Get the latest increment ID # new_id = cursor.lastrowid
3, access to query data
import pymysql conn = pymysql.connect(host='127.0.0.1', port=3306, user='root', passwd='123', db='t1') cursor = conn.cursor() cursor.execute("select * from hosts") # Get first row of data row_1 = cursor.fetchone() # Get the first n rows of data # row_2 = cursor.fetchmany(3) # Get all data # row_3 = cursor.fetchall() conn.commit() cursor.close() conn.close()
fetchone () method can help us get the data in the table, but each execution cur.fetchone () the data obtained are not the same, in other words I did once, the cursor will move from the table of the first data to the next a location data, so when I get is again performed the second data. either fetchone () function returns the value of a single tuple, i.e. a row, if not, return null
or fetchall () function returns the value of a plurality of tuples, i.e., a plurality of rows returned, if not, returns (),
Note: the order in the data fetch time, it may be used cursor.scroll (num, mode) to move the cursor position, such as:
- cursor.scroll (1, mode = 'relative') # relative to the current position of the mobile
- cursor.scroll (2, mode = 'absolute') # absolute relative position
4, fetch data type
The default is to obtain data on the ancestral type, or if you want to type the data dictionary, namely:
import pymysql conn = pymysql.connect(host='127.0.0.1', port=3306, user='root', passwd='123', db='t1') # Cursor is set to a dictionary cursor = conn.cursor(cursor=pymysql.cursors.DictCursor) r = cursor.execute("call p1()") result = cursor.fetchone() conn.commit() cursor.close() conn.close()
5, insert data
By the above execute () method writes the pure sql statement is not convenient to insert data. Such as:
>>>cur.execute("insert into student values('2','Tom','3 year 2 class','9')")
I want to insert new data, these values have to make changes in the statement. We can make the following modifications:
#coding=utf-8 import MySQLdb conn= MySQLdb.connect( host='localhost', port = 3306, user='root', passwd='123456', db ='test', ) cur = conn.cursor() # Insert a data sqli="insert into student values(%s,%s,%s,%s)" cur.execute(sqli,('3','Huhu','2 year 1 class','7')) cur.close() conn.commit() conn.close()
If you want to insert more than one value according to the data in the table it?
executemany () method can be inserted into a plurality of values, the sql statement singled, but is repeatedly performed in the parameter list of parameters, number of rows returned value is affected.
#coding=utf-8 import MySQLdb conn= MySQLdb.connect( host='localhost', port = 3306, user='root', passwd='123456', db ='test', ) cur = conn.cursor() # Insert more than one record sqli="insert into student values(%s,%s,%s,%s)" cur.executemany(sqli,[ ('3','Tom','1 year 1 class','6'), ('3','Jack','2 year 1 class','7'), ('3','Yaheng','2 year 2 class','7'), ]) cur.close() conn.commit() conn.close()
6. Exercise python MySQL operation
#!/usr/bin/env python # coding=utf-8 import pymysql def connectdb(): print ( '... are connected to the server mysql') # Open Database Connectivity # Username: hp, password:. Hp12345, user name and password need to change your mysql user name and password, and you want to create a database TESTDB, and created a database table Student in TESTDB db = pymysql.connect("localhost","hp","Hp12345.","TESTDB") print ( 'connected on!') return db def createtable(db): # Use cursor () method to get the cursor operation cursor = db.cursor() # If there is to delete the table Sutdent cursor.execute("DROP TABLE IF EXISTS Student") sql = """CREATE TABLE Student ( ID CHAR(10) NOT NULL, Name CHAR(8), Grade INT )""" # Create a table Sutdent cursor.execute(sql) def insertdb(db): # Use cursor () method to get the cursor operation cursor = db.cursor() # SQL insert statement sql = """INSERT INTO Student VALUES ('001', 'CZQ', 70), ( '002', 'LHQ', 80), ( '003', 'MQ', 90), ('004', 'WH', 80), ('005', 'HP', 70), ('006', 'YF', 66), ('007', 'TEST', 100)""" #sql = "INSERT INTO Student(ID, Name, Grade) \ # VALUES ('%s', '%s', '%d')" % \ # ('001', 'HP', 60) try: # Sql statement execution cursor.execute(sql) # Submitted to the database to perform db.commit() except: # Rollback in case there is any error print 'insert data failed!' db.rollback() def querydb(db): # Use cursor () method to get the cursor operation cursor = db.cursor() # SQL query #sql = "SELECT * FROM Student \ # WHERE Grade > '%d'" % (80) sql = "SELECT * FROM Student" try: # Execute SQL statements cursor.execute(sql) # Get a list of all the records results = cursor.fetchall() for row in results: ID = row[0] Name = row[1] Grade = row[2] # Print results print "ID: %s, Name: %s, Grade: %d" % \ (ID, Name, Grade) except: print "Error: unable to fecth data" def deletedb(db): # Use cursor () method to get the cursor operation cursor = db.cursor() # SQL delete statement sql = "DELETE FROM Student WHERE Grade = '%d'" % (100) try: # Execute SQL statements cursor.execute(sql) # Commit the changes db.commit() except: print 'delete data failed!' # Rollback when an error occurs db.rollback() def updatedb(db): # Use cursor () method to get the cursor operation cursor = db.cursor() # SQL update statement sql = "UPDATE Student SET Grade = Grade + 3 WHERE ID = '%s'" % ('003') try: # Execute SQL statements cursor.execute(sql) # Submitted to the database to perform db.commit() except: print 'update data failed!' # Rollback when an error occurs db.rollback() def closedb(db): db.close() def main(): db = connectdb () # MySQL database connection createtable (db) # Create a table insertdb (db) # insert data print '\ n inserting data:' querydb(db) deletedb (db) # delete data print '\ n delete the data:' querydb(db) updatedb (db) # update data print '\ n update data:' querydb(db) closedb (db) # close the database if __name__ == '__main__': main()
Sixth, the consolidation exercise
Exercises:
Reference Table Structure:
User Type
User Info
Competence
User Type & Permissions
function:
# Log in, register, retrieve password
# User Management
# User type
# privilege management
# assign permissions
Special: only a program executable file
python python operation SQLite
SQLite is an embedded database, it is a database file. Since SQLite itself is written in C, and very small. So, often integrated into a variety of applications, and even can be integrated in iOS and Android APP.
Python on the built-SQLite3, so in python using SQLite, no need to install anything directly.
Before using SQLite, we need to clear up some concepts:
Table is stored in the database relational data collection, a database which usually contain multiple tables, such as student performance, class tables, school tables and so on. By the foreign key between the tables.
To operate a relational database, first need to connect to the database, a database connected as Connection;
After connecting to the database, need to open the cursor, called Cursor, execute SQL statements Cursor, and then obtains the result.
Python API interface defines a set of database operations, Python be connected to any database, the database need only provide the drive to meet the standard Python.
Since the drive built-in SQLite Python standard library, so we can directly operate SQLite database.
We practice it in line Python interactive command:
# Import SQLite driver: >>> import sqlite3 # Connect to the SQLite database # Database file is test.db # If the file does not exist, it will be automatically created in the current directory: >>> conn = sqlite3.connect('test.db') # Create a Cursor: >>> cursor = conn.cursor() # Execute a SQL statement, create a user table: >>> cursor.execute('create table user (id varchar(20) primary key, name varchar(20))') <sqlite3.Cursor object at 0x10f8aa260> # Continue to execute a SQL statement to insert a record: >>> cursor.execute('insert into user (id, name) values (\'1\', \'Michael\')') <sqlite3.Cursor object at 0x10f8aa260> # Get the number of rows inserted through rowcount: >>> cursor.rowcount 1 # Close Cursor: >>> cursor.close() # Commit the transaction: >>> conn.commit() # Close Connection: >>> conn.close()
We'll try Search record:
>>> conn = sqlite3.connect('test.db') >>> cursor = conn.cursor() # Execute a query: >>> cursor.execute('select * from user where id=?', ('1',)) <sqlite3.Cursor object at 0x10f8aa340> # Obtain a query result set: >>> values = cursor.fetchall() >>> values [('1', 'Michael')] >>> cursor.close() >>> conn.close()
When using Python's DB-API, as long as the clear connection and cursor objects, remember to turn off after a certain open, you can rest assured that use.
When performing insert, update, delete statement using the cursor object, the results returned by the number of rows affected rowcount, you can get the results.
Select statement is executed using cursor object by featchall () can get the result set, the result set is a list, each element is a tuple, corresponds to one row.
If the SQL statement with parameters, you need to pass parameters in accordance with the position to execute () method, there are several? Placeholder must correspond to several parameters, for example:
ursor.execute('select * from user where name=? and pwd=?', ('abc', 'password'))
Exercise:
import os, sqlite3 db_file = os.path.join(os.path.dirname(__file__), 'test.db') print(db_file) # E:/backup/pycode/now/ProcessDataPreprocessing/code\test.db if os.path.isfile(db_file): os.remove(db_file) # Initialization data conn = sqlite3.connect(db_file) cursor = conn.cursor() sql1 = 'create table user(id varchar(20) primary key , name varchar(20), score int)' cursor.execute(sql1) sql2 = "insert into user values ('001','james', 99)" cursor.execute(sql2) sql3 = "insert into user values ('002','durant', 99)" cursor.execute(sql3) cursor.close() conn.commit() conn.close()
Query:
# Query the records: conn = sqlite3.connect('test.db') cursor = conn.cursor() # Execute a query: sql4 = 'select * from user ' cursor.execute(sql4) # Obtain a query result set: values = cursor.fetchall() print(values) cursor.close() conn.close()