简体   繁体   English

在Python中使用sqlite3连接到本地主机上的数据库

[英]Connect to Database on local host with sqlite3 in python

I have a database that I am running on my local machine which I can access through Microsoft SQL Server Manager Studio. 我有一个在本地计算机上运行的数据库,可以通过Microsoft SQL Server Manager Studio访问该数据库。 I connect to this server "JIMS-LAPTOP\\SQLEXPRESS" and then I can run queries through the manager. 我连接到该服务器“ JIMS-LAPTOP \\ SQLEXPRESS”,然后可以通过管理器运行查询。 However I need to be able to connect to this database and work with it through python. 但是我需要能够连接到该数据库并通过python使用它。 When I try to connect using sqlite3 like 当我尝试使用sqlite3进行连接时

conn = sqlite3.connect("JIMS-LAPTOP\SQLEXPRESS")

I get an unable to open database file error 我收到无法打开数据库文件的错误

I tried accessing the temporary file directly like this 我尝试像这样直接访问临时文件

conn = sqlite3.connect("C:\Users\Jim Notaro\AppData\Local\Temp\~vs13A7.sql")
c = conn.cursor()
c.execute("SELECT name FROM sqlite_master WHERE type = \"table\"")
print c.fetchall()

Which allows me to access a database but it is completely empty (No tables are displayed) 这使我可以访问数据库,但是它完全是空的(不显示任何表)

I also tried connecting like this 我也尝试这样连接

conn = sqlite3.connect("SQL SERVER (SQLEXPRESS)")

Which is what the name is in the sql server configuration manager but that also returns a blank database. 这是sql服务器配置管理器中的名称,但它还会返回一个空白数据库。

I'm not sure how I am suppose to be connecting to the database using python 我不确定我应该如何使用python连接到数据库

You can't use sqlite3 to connect to SQL server, only to Sqlite databases. 您不能使用sqlite3连接到SQL Server,只能使用Sqlite数据库。 You need to use a driver that can talk to MS SQL, like pyodbc . 您需要使用可以与MS SQL通讯的驱动程序,例如pyodbc

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM