Guida SQLite Con Python
Guida SQLite Con Python
import sqlite3
from sqlite3 import Error
def create_connection(db_file):
""" create a database connection to a SQLite database """
conn = None
try:
conn = sqlite3.connect(db_file)
print(sqlite3.version)
except Error as e:
print(e)
finally:
if conn:
conn.close()
if __name__ == '__main__':
create_connection(r"C:\sqlite\db\pythonsqlite.db")
In questo codice:
import sqlite3
from sqlite3 import Error
def create_connection():
""" create a database connection to a database that resides
in the memory
"""
conn = None;
try:
conn = sqlite3.connect(':memory:')
print(sqlite3.version)
except Error as e:
print(e)
finally:
if conn:
conn.close()
if __name__ == '__main__':
create_connection()
In questo tutorial, hai imparato come creare un database SQLite su
disco e in memoria da un programma Python utilizzando il modulo
sqlite3.
SQLite Python: creazione di tabelle
-- projects table
CREATE TABLE IF NOT EXISTS projects (
id integer PRIMARY KEY,
name text NOT NULL,
begin_date text,
end_date text
);
-- tasks table
CREATE TABLE IF NOT EXISTS tasks (
id integer PRIMARY KEY,
name text NOT NULL,
priority integer,
project_id integer NOT NULL,
status_id integer NOT NULL,
begin_date text NOT NULL,
end_date text NOT NULL,
FOREIGN KEY (project_id) REFERENCES projects (id)
);
def create_connection(db_file):
""" create a database connection to the SQLite database
specified by db_file
:param db_file: database file
:return: Connection object or None
"""
conn = None
try:
conn = sqlite3.connect(db_file)
return conn
except Error as e:
print(e)
return conn
def main():
database = r"C:\sqlite\db\pythonsqlite.db"
# create tables
if conn is not None:
# create projects table
create_table(conn, sql_create_projects_table)
if __name__ == '__main__':
main()
import sqlite3
from sqlite3 import Error
def create_connection(db_file):
""" create a database connection to the SQLite database
specified by db_file
:param db_file: database file
:return: Connection object or None
"""
conn = None
try:
conn = sqlite3.connect(db_file)
return conn
except Error as e:
print(e)
return conn
def main():
database = r"C:\sqlite\db\pythonsqlite.db"
# create tables
if conn is not None:
# create projects table
create_table(conn, sql_create_projects_table)
if __name__ == '__main__':
main()
sqlite> .tables
projects tasks
def create_connection(db_file):
""" create a database connection to the SQLite database
specified by db_file
:param db_file: database file
:return: Connection object or None
"""
conn = None
try:
conn = sqlite3.connect(db_file)
except Error as e:
print(e)
return conn
return cur.lastrowid
def main():
database = r"C:\sqlite\db\pythonsqlite.db"
# tasks
task_1 = ('Analyze the requirements of the app', 1, 1,
project_id, '2015-01-01', '2015-01-02')
task_2 = ('Confirm with user about the top requirements', 1, 1,
project_id, '2015-01-03', '2015-01-05')
# create tasks
create_task(conn, task_1)
create_task(conn, task_2)
E chiama la main()funzione:
if __name__ == '__main__':
main()
import sqlite3
from sqlite3 import Error
def create_connection(db_file):
""" create a database connection to the SQLite database
specified by db_file
:param db_file: database file
:return: Connection object or None
"""
conn = None
try:
conn = sqlite3.connect(db_file)
except Error as e:
print(e)
return conn
def main():
database = r"C:\sqlite\db\pythonsqlite.db"
# tasks
task_1 = ('Analyze the requirements of the app', 1, 1,
project_id, '2015-01-01', '2015-01-02')
task_2 = ('Confirm with user about the top requirements', 1, 1,
project_id, '2015-01-03', '2015-01-05')
# create tasks
create_task(conn, task_1)
create_task(conn, task_2)
if __name__ == '__main__':
main()
sqlite> .header on
sqlite> .mode column
In questo tutorial, hai imparato come inserire righe nelle tabelle nel
database SQLite da un programma Python.
SQLite Python: modifica dei dati
def create_connection(db_file):
""" create a database connection to the SQLite database
specified by the db_file
:param db_file: database file
:return: Connection object or None
"""
conn = None
try:
conn = sqlite3.connect(db_file)
except Error as e:
print(e)
return conn
def main():
database = r"C:\sqlite\db\pythonsqlite.db"
if __name__ == '__main__':
main()
Ecco il programma completo:
import sqlite3
from sqlite3 import Error
def create_connection(db_file):
""" create a database connection to the SQLite database
specified by the db_file
:param db_file: database file
:return: Connection object or None
"""
conn = None
try:
conn = sqlite3.connect(db_file)
except Error as e:
print(e)
return conn
def main():
database = r"C:\sqlite\db\pythonsqlite.db"
if __name__ == '__main__':
main()
sqlite> .header on
sqlite> .mode column
return conn
def select_all_tasks(conn):
"""
Query all rows in the tasks table
:param conn: the Connection object
:return:
"""
cur = conn.cursor()
cur.execute("SELECT * FROM tasks")
rows = cur.fetchall()
rows = cur.fetchall()
def main():
database = r"C:\sqlite\db\pythonsqlite.db"
import sqlite3
from sqlite3 import Error
def create_connection(db_file):
""" create a database connection to the SQLite database
specified by the db_file
:param db_file: database file
:return: Connection object or None
"""
conn = None
try:
conn = sqlite3.connect(db_file)
except Error as e:
print(e)
return conn
def select_all_tasks(conn):
"""
Query all rows in the tasks table
:param conn: the Connection object
:return:
"""
cur = conn.cursor()
cur.execute("SELECT * FROM tasks")
rows = cur.fetchall()
rows = cur.fetchall()
def main():
database = r"C:\sqlite\db\pythonsqlite.db"
def create_connection(db_file):
""" create a database connection to the SQLite database
specified by the db_file
:param db_file: database file
:return: Connection object or None
"""
conn = None
try:
conn = sqlite3.connect(db_file)
except Error as e:
print(e)
return conn
def delete_all_tasks(conn):
"""
Delete all rows in the tasks table
:param conn: Connection to the SQLite database
:return:
"""
sql = 'DELETE FROM tasks'
cur = conn.cursor()
cur.execute(sql)
conn.commit()
Questa main() funzione chiama la create_connection() funzione e la
delete_task() funzione per eliminare l'attività con ID 2 dalla tasks
tabella:
def main():
database = r"C:\sqlite\db\pythonsqlite.db"
if __name__ == '__main__':
main()
import sqlite3
from sqlite3 import Error
def create_connection(db_file):
""" create a database connection to the SQLite database
specified by the db_file
:param db_file: database file
:return: Connection object or None
"""
conn = None
try:
conn = sqlite3.connect(db_file)
except Error as e:
print(e)
return conn
def delete_all_tasks(conn):
"""
Delete all rows in the tasks table
:param conn: Connection to the SQLite database
:return:
"""
sql = 'DELETE FROM tasks'
cur = conn.cursor()
cur.execute(sql)
conn.commit()
def main():
database = r"C:\sqlite\db\pythonsqlite.db"