Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a new database using python and sqlite3

Tags:

python

sqlite

import sqlite3  conn = sqlite3.connect(r"D:\aaa.db") 

Is there a way to automatically create the db file if it doesn't already exist when I connect to it?

like image 910
zjm1126 Avatar asked Feb 03 '10 03:02

zjm1126


People also ask

How do I create a SQLite database and table in Python?

SQLite Python: Creating Tables First, create a Connection object using the connect() function of the sqlite3 module. Second, create a Cursor object by calling the cursor() method of the Connection object. Third, pass the CREATE TABLE statement to the execute() method of the Cursor object and execute this method.

How does Python integrate with SQLite?

Connect To Database#!/usr/bin/python import sqlite3 conn = sqlite3. connect('test. db') print "Opened database successfully"; Here, you can also supply database name as the special name :memory: to create a database in RAM.


1 Answers

The code you give does create 'D:\\aaa.db' if it doesn't exist.

like image 176
Alex Martelli Avatar answered Nov 15 '22 13:11

Alex Martelli