Home > News list > Programming/Database >> Database Operation Tutorial

How to use Pycharm to connect to SQL Server (detailed tutorial)

Database Operation Tutorial 2023-05-12 12:12:22 Source: Network

SQL Sever1433 pycharmpycharmSQL SeverTipSQL Sever: ------------------------------   LAPTOP-40O6HVDS  ------------------------------ :   (provider: Shared Memory Provider, error: 0 - ) (Microsoft SQL Server: 233)   SQL Sever SQL Sever Window create database soft2104soft2104soft2104pycharmuse master --drop database soft2104 --  yunmengzepycharmSQL Sever 123456123456pycharm  db_ower  SQL Sever  SQL Sever: ------------------------------   LAPTOP-40O6HVDS  ------------------------------ :   (provider: Shared Memory Provider, error: 0 - ) (Microsoft SQL Server: 233)  1433 1433 1433winRcmdtelnet localhost  1433'telnet' winRcontrol Windows Telnet1winRcmdtelnet localhost  1433localhost.

pycharmSQL Sever

Tip

SQL Sever

: ------------------------------   LAPTOP-40O6HVDS  ------------------------------ :   (provider: Shared Memory Provider, error: 0 - ) (Microsoft SQL Server: 233)  

 SQL Sever

 

SQL Sever Window

create database soft2104

soft2104soft2104pycharm

use master --drop database soft2104 -- 

 

yunmengzepycharm

SQL Sever 123456123456pycharm

 

 

db_ower

 

 

SQL Sever

 SQL Sever

: ------------------------------   LAPTOP-40O6HVDS  ------------------------------ :   (provider: Shared Memory Provider, error: 0 - ) (Microsoft SQL Server: 233)  

1433

1433

1433

winRcmd

telnet localhost  1433

'telnet'

winRcontrol

 Windows

Telnet1

winRcmdtelnet localhost  1433

localhost... 1433:

sql

 

 

SQL SeverMSSQLSEVER

 TCP/IPTCP/IP

IP

 IP1IP2TCP1433

IPAllTCP1433

 SQL SeverSQL Sever

winRcmdtelnet localhost  1433 1433

pycharm

pycharmpycharmSQL Sever

pip install pymssql

pymssqlpy

import pymssql connect = pymssql.connect(host='localhost',server='LAPTOP-40O6HVDS', port='1433',user='yunmengze', password= '123456', database='soft2104')  # host,server, port,user, password, databaseif connect:

'yunmengze', '123456', 'soft2104'

LAPTOP-40O6HVDSSQL Sever

import pymssqlconnect = pymssql.connect(host='localhost',server='LAPTOP-40O6HVDS', port='1433',user='yunmengze', password= '123456', database='soft2104',charset="GBK")  # host,server, port,user, password, database,charsetif connect:    print("")else:    print("")

pycharmSQL Sever

pycharmSQL Sever

SQL Severpycharm……

charset="GBK"GBK

 

 

………………

sql sever

pycharm

sql severoldservernamenewservername

/*select @@SERVERNAME--select SERVERPROPERTY('ServerName')--*/sp_helpserver    ----oldservernamesp_dropserver 'oldservername' --newservernamesp_addserver 'newservername','local' --newservernamesp_serveroption 'newservername', 'data access', 'on'--oldservernamenewservername

SQL Severpycharm

sql severbulk

bulk insert soft2110 --txtsoft2110from 'd:2104.txt'   --txtDANSIwith(fieldterminator = 't',rowterminator = 'n')         --tn

 yunmengzesysadmin

sql sever

nvarcharvarcharGBKpycharmpycharmutf-8tablenchar(20)utf-8202

use soft2104create table soft2110(xh int,dp nchar(20),pr nchar(20),id nchar(30),nm nchar(20))

soft2110GBKnvarcharvarcharsoft2110

drop table soft2110

use soft2104drop table soft2110create table soft2110(xh int,dp nchar(20),pr nchar(20),id nchar(30),nm nchar(20)) bulk insert soft2110 --txt textfrom 'd:2104.txt'with(fieldterminator = 't',rowterminator = 'n')--tdelete from soft2110 where xh=71

D2104.txt

1        2204    31212121212   
2        2204    31313131313   

D2104.txtANSI

sql severexecute

pycharm

cur = connect.cursor()sqlstr = "select * from soft2110"cur.execute(sqlstr)data = cur.fetchall()cur.close()for i in range(len(data)):    for t in range(len(data[i])):        print(str(data[i][t]).strip(), end='  ')    print('r')connect.close()

connect.close()sql sever cur.close()   

1        2204    31212121212   
2        2204    31313131313   

datafor 

[(1,,2204,31212121212,),(2,,2204,31313131313,)]

        cur.execute(sqlstr)
        data = cur.fetchall()

        cur.execute(sqlstr)
        connect.commit()

print("  2204  349849848  ")add = input('    ':')try:    cur = connect.cursor()    sqlstr = "select * from soft2110"    cur.execute(sqlstr)    data = cur.fetchall()    sqlstr = f"select * from soft2110 where id = '{add[2]}'"    cur.execute(sqlstr)    acc = cur.fetchall()    if acc:        print("")        return    try:        sqlstr =f"insert into soft2110 values ({len(data) + 1},'{add[0]}','{add[1]}','{add[2]}','{add[3]}')"        # print(sqlstr)        cur.execute(sqlstr)        connect.commit()        print("")    except:        print("")    cur.close()    connect.close()except:    print("")

Tip

sqlstr = f"select * from soft2110 where id = '{add[2]}'"

f "  " { } { } f { } add[2]

print(f"{2*3}")

6

print("{2*3}")

{2*3}

de = input(':')
sqlstr = f"delete from soft2110 where id = '{de}'"

def delete(connect, de):    cur = connect.cursor()    sqlstr = f"select * from soft2110 where id = '{de}'"    cur.execute(sqlstr)    acc = cur.fetchall()    if acc:        sqlstr = f"delete from soft2110 where id = '{de}'"        cur.execute(sqlstr)        connect.commit()        cur.close()        print("")    else:        print("")        cur.close()    returnde = input(':')try:    delete(connect, de)except Exception as e:    print(f"Error: {e}")

 execute(  ) pythontryexcept

PycharmSQL Sever,PycharmSQL Sever

Tag: to How use Pycharm connect SQL Server detailed tutorial


Disclaimer: The content of this article is sourced from the internet. The copyright of the text, images, and other materials belongs to the original author. The platform reprints the materials for the purpose of conveying more information. The content of the article is for reference and learning only, and should not be used for commercial purposes. If it infringes on your legitimate rights and interests, please contact us promptly and we will handle it as soon as possible! We respect copyright and are committed to protecting it. Thank you for sharing.

AdminSo

http://www.adminso.com

Copyright @ 2007~2024 All Rights Reserved.

Powered By AdminSo

Open your phone and scan the QR code on it to open the mobile version


Scan WeChat QR code

Follow us for more hot news

AdminSo Technical Support