简体   繁体   中英

Creating a database connection in Python using SQLite

I am new at SQLite in Python and I am trying to create a database connection.

I have the following as a datbase location:

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

The database is called FTHeader. However, when I try it i get an error saying 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 :

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.

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')''')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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