Python connects and simply operates the SQL server database

environment:

pycharm, SQLserver version 2019

1. First, click File in pycharm, find setting——project: ***, click "+" to import the pymssql library

2. Write code to connect to the database, and perform simple operations such as querying the database (only query is shown here)

import pymssql
connect=pymssql.connect(server='DESKTOP-08O3C8T',user='sa',password='***',database='1234')
#服务器名,账号,密码,数据库名
if connect:
    print("连接数据库成功!")

#查询
cursor=connect.cursor()
sql_select="select * from SC"
cursor.execute(sql_select)#执行sql语句
results=cursor.fetchall()#读取所有查询结果
for result in results: #循环读取所有结果
    result=list(result)
    for res in range(len(result)):
        if isinstance(result[res],str):
            result[res]=result[res].replace(' ','')
    result=tuple(result)
    print("处理后:",end='')
    print(result)
connect.commit()
#关闭数据库
connect.close()

 3. In the sql server, because the windows authentication mode was used to enter the database manager before, the authentication must be modified and changed to sql authentication to connect to the database:

Authentication modification

(1) First log in to the database with Windows authentication, right-click the root directory - Properties - Security - check the SQL and windows authentication mode;

(2) Select security under the root directory - login name - sa - right-click properties - modify password and cancel the enforcement of the password policy; check Grant and Enable in the status option; (One thing to note is that no matter how many digits you change in the password, it will always show fifteen digits in the above picture.)

(3) Configuration protocol: Find the configuration tool SQL Server Configuration Manager program in the database, and change the status of Named Pipes and TCP/IP in the SQL Server2005 network configuration to enabled. Then log in using SQL server authentication. If you can't find the SQL Server Configuration Manager program, you can right-click on this computer - Manage - Sql Configuration Manager:

(4) After you have changed all these, you need to restart Sql Server Management Studio, pay attention to restart! ! ! Instead of closing and opening the database, right-click the root directory-restart! !

result:

 Supplement: Chinese garbled characters will appear in the above simple query code query

For example, query the student table:

method one:

Add charset='cp936' in pymqsql.connect()

Reason: Because the string field type is: varchar, the read data is garbled

Disadvantage: If the sql statement contains Chinese, the data will not be obtained

Method 2: (more common)

Directly use sql statement to convert varchar to nvarchar type, no longer need to specify charset, there is no problem (note that the column name is converted to an alias), directly write sql statement as follows

sql="select Sno,convert(nvarchar(50),Sname) as 'a',
           convert(nvarchar(50),Ssex) as 'b' from student'

The result after processing is as follows:

Guess you like

Origin blog.csdn.net/m0_67234599/article/details/130535166