Python study notes: Python SQL Server database operations

      Recently write data to the database, I learned how to use Python to manipulate SQL Server databases.

First, connect to the database:

      First of all, we want to connect to SQL Server database, you need to install pymssql this third-party libraries, open cmd, enter the following command, wait for the installation to complete.

pip install pymssql

      The code connecting to the database as follows:

Import pymssql 

Print ( ' Start Connect to Database ' ) 
Connect = pymssql.connect ( ' localhost ' , ' SA ' , ' 123456 ' , ' BackupTest_1 ' ) # database instance name / address, user name, password, the name of the database to be connected 
Print ( ' Connecting ' )
 IF Connect:
     Print ( ' the DataBase Connect Success ' ) 
cursor = connect.cursor (); # Get cursor operation

      Output:

 

Second, using a cursor operation database

① create a data table:

= SQL "" " 
the IF the OBJECT_ID ( 'Student', 'the U-') the NOT NULL the IS the DROP TABLE Student 
the CREATE TABLE Student (the INT the NOT NULL ID Identity (1,1), the Name NVARCHAR (100), the INT Age, a PRIMARY KEY (ID )) 
"" " 
cursor.execute (sql) # execute sql statement 
connect.commit () # submitted to the database to perform

Execution results are as follows:

 

② insert data:

= insert_sql "" " 
the INSERT the INTO Student (the Name, Age) 
the VALUES (% S,% D) 
" "" 
data_insert = [( ' John Doe ' , 21), ( ' John Doe ' , 22)] # claim tuple type is array 
cursor.executemany (insert_sql, data_insert) 
connect.commit ()

Execution results are as follows:

 

③ query data:

= sql_select " the SELECT * the FROM Student " 
cursor.execute (sql_select) 
the Result = cursor.fetchall () # use the cursor to obtain all the results 
Print (the Result)

Execution results are as follows:

Note: In addition to or fetchall (), there are either fetchone () and fetchmany () method gets the data and the like. However, the cursor will record the first few records to get results, so when you're done with the implementation of the above fetchall (), then execute fetchone () time and print the results, you will find it is a null value.

 

④ modify the data:

# The query statement with Chinese remember to add N 
sql_modify = "" " 
the UPDATE Student the SET the WHERE Age = 24 N = the Name 'John Doe' 
" "" 
the cursor.execute (sql_modify) 
connect.commit () 
the cursor.execute ( " SELECT * FROM Student WHERE Name = N ' John Doe' ' ) # remember added N 
search_result = cursor.fetchall ()
 Print (search_result)

Execution results are as follows, Joe Smith from the age of 21 was changed to 24:

Note: Because I loom environment is English, the construction of the table when the value of the type field set to Chinese NVACHAR, and when the query field as required in front of the condition value string (not included in the string) plus a N, otherwise no effect after the SQL statement is executed, (0 row (s) affected )

 

⑤ delete the data:

cursor.execute("SELECT * FROM Student")
search_before = cursor.fetchall()
print(search_before)
sql_delete = """
DELETE FROM Student WHERE Name = N'张三'
"""
cursor.execute(sql_delete)
connect.commit()
cursor.execute("SELECT * FROM Student")
search_after = cursor.fetchall()
print(search_after)

Execution results are as follows:

 

⑥ close the database connection:

connect.close()

 

to sum up:

      The above is how to use Python SQL Server database connection operations (additions and deletions to change search), it is actually a reason for other databases (MySQL, SQLite, etc.), but need to import the corresponding package it, SQL syntax is similar. Benpian simply describes how data and access the database in Python, next blog will talk about how to get the whole Python database tables (including table structure), and to draw statistical graphs using the rich tool Python library.

Guess you like

Origin www.cnblogs.com/Sunny20181123/p/10929587.html