如何使用python连接到SQL Server数据库?

时间:2022-04-22 10:23:46

Im trying to conennect to an sql database that its already created an that its located on a server. How can I connect to this database using python. Ive tried using java but I cant seem to get working either.

我试图连接到一个sql数据库,它已经创建了一个位于服务器上的数据库。如何使用python连接到此数据库。香港专业教育学院尝试使用java,但我似乎无法工作。

2 个解决方案

#1


7  

Well depending on what sql database you are using you can pip install pymssql for microsoft sql (mssql), psycopg2 for postgres (psql) or mysqldb for mysql databases Here are a few examples of using it

那么根据你使用的sql数据库你可以pip安装pymssql for microsoft sql(mssql),psycopg2 for postgres(psql)或mysqldb for mysql databases以下是一些使用它的例子

Microsoft sql

import pymssql

conn = pymssql.connect(server=server, user=user, password=password, database=db)
cursor = conn.cursor()

cursor.execute("SELECT COUNT(MemberID) as count FROM Members WHERE id = 1")
row = cursor.fetchone()

conn.close()

print(row)

Postgres

import psycopg2

conn = psycopg2.connect(database=db, user=user, password=password, host=host, port="5432")
cursor = conn.cursor()

cursor.execute('SELECT COUNT(MemberID) as count FROM Members WHERE id = 1')
row = cursor.fetchone()

conn.close()

print(row)

mysql

import MySQLdb

conn = MySQLdb.connect(host=host, user=user, passwd=passwd, db=db)
cursor = conn.cursor()

cursor.execute('SELECT COUNT(MemberID) as count FROM Members WHERE id = 1')
row = cursor.fetchone()

conn.close()

print(row)

#2


-1  

In order to connect to SQL using python, a module such as SQLite must be used. Here is an example script to use Python3 and SQLite3 to connect to a database...

为了使用python连接到SQL,必须使用SQLite等模块。这是一个使用Python3和SQLite3连接数据库的示例脚本...

import sqlite3  #import the required module.

conn = sqlite3.connect("*Database_Name.db*")  
   #create the var. 'conn' and connect to a database the correct name.  If the 
   database does not exist, then one will be created with that name.
c = conn.cursor() 
   #Create a var. c to hold the cursor, which is used to interact with the 
   database.
c.execute("*SQL Code Here*")
   #Use the cursor var. to interact with the database.  Replace the fake 
   code with an actual SQL query.
conn.commit()  
   #Calling commit to that database connection is kind of like saving the 
   changes that were made to the database.
conn.close()
   #Be sure to close the connection to the database.

#1


7  

Well depending on what sql database you are using you can pip install pymssql for microsoft sql (mssql), psycopg2 for postgres (psql) or mysqldb for mysql databases Here are a few examples of using it

那么根据你使用的sql数据库你可以pip安装pymssql for microsoft sql(mssql),psycopg2 for postgres(psql)或mysqldb for mysql databases以下是一些使用它的例子

Microsoft sql

import pymssql

conn = pymssql.connect(server=server, user=user, password=password, database=db)
cursor = conn.cursor()

cursor.execute("SELECT COUNT(MemberID) as count FROM Members WHERE id = 1")
row = cursor.fetchone()

conn.close()

print(row)

Postgres

import psycopg2

conn = psycopg2.connect(database=db, user=user, password=password, host=host, port="5432")
cursor = conn.cursor()

cursor.execute('SELECT COUNT(MemberID) as count FROM Members WHERE id = 1')
row = cursor.fetchone()

conn.close()

print(row)

mysql

import MySQLdb

conn = MySQLdb.connect(host=host, user=user, passwd=passwd, db=db)
cursor = conn.cursor()

cursor.execute('SELECT COUNT(MemberID) as count FROM Members WHERE id = 1')
row = cursor.fetchone()

conn.close()

print(row)

#2


-1  

In order to connect to SQL using python, a module such as SQLite must be used. Here is an example script to use Python3 and SQLite3 to connect to a database...

为了使用python连接到SQL,必须使用SQLite等模块。这是一个使用Python3和SQLite3连接数据库的示例脚本...

import sqlite3  #import the required module.

conn = sqlite3.connect("*Database_Name.db*")  
   #create the var. 'conn' and connect to a database the correct name.  If the 
   database does not exist, then one will be created with that name.
c = conn.cursor() 
   #Create a var. c to hold the cursor, which is used to interact with the 
   database.
c.execute("*SQL Code Here*")
   #Use the cursor var. to interact with the database.  Replace the fake 
   code with an actual SQL query.
conn.commit()  
   #Calling commit to that database connection is kind of like saving the 
   changes that were made to the database.
conn.close()
   #Be sure to close the connection to the database.