简体   繁体   English

使用SQLite在Python中创建数据库连接

[英]Creating a database connection in Python using SQLite

I am new at SQLite in Python and I am trying to create a database connection. 我是Python的SQLite的新手,正在尝试创建数据库连接。

I have the following as a datbase location: 我将以下内容作为datbase位置:

E:\Program Files\Microsoft SQL Server\MSSQL10_50.MSSQLSERVER\MSSQL\DATA\master.mdf (i think) 

The database is called FTHeader. 该数据库称为FTHeader。 However, when I try it i get an error saying unable to open database file 但是,当我尝试它时,我收到一条错误消息,提示unable to open database file

any help would be greatly appreciated. 任何帮助将不胜感激。

Pick your poison from here and import the module as db and the following should sort you, cribbing from PEP 249 : 从这里选择您的毒药 ,并将模块作为db导入,以下内容将对您进行分类,从PEP 249开始

import adodbapi as db
import adodbapi.ado_consts as db.consts
Cfg={‘server’:’192.168.29.86\\eclexpress’,‘password’:‘xxxx’,‘db’:‘pscitemp’}
constr = r”Provider=SQLOLEDB.1; Initial Catalog=%s; Data Source=%s; user ID=%s; Password=%s; “ \
% (Cfg['db'], Cfg['server'], ‘sa’, Cfg['password'])
conn=db.connect(constr)

You should now be connected to your database after replacing the Cfg dictionary to match your installation. 在替换Cfg字典以匹配您的安装之后,现在应该连接到数据库。

I have created a database when completing a controlled assesment, here is the code that I have used: 完成受控评估后,我已经创建了一个数据库,这是我使用的代码:

import sqlite3

new_db = sqlite3.connect ('R:\\subjects\\Computing & ICT\\Student Area\\Y11\\Emilyc\\results1.db')
c=new_db.cursor()
c.execute('''CREATE TABLE results1
(
    results1_name text,
    results1_class number,
    quiz_score number)
    ''')

#Class 1
c.execute('''INSERT INTO results1
VALUES ('John Watson','1','5')''')

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

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