0
Could someone help me on how to create a dynamic database, where I can add and remove items? In Python, if possible with Sqlite
0
Could someone help me on how to create a dynamic database, where I can add and remove items? In Python, if possible with Sqlite
2
In the example below, the Sqlite database is created in the file foobar.db
, where a table called tb_fruta
is created and its items manipulated (inclusion, alteration and exclusion) through commands SQL
:
import sqlite3
conn = sqlite3.connect('foobar.db')
c = conn.cursor()
c.execute("CREATE TABLE tb_fruta ( id integer, nome text );")
c.execute("INSERT INTO tb_fruta ( id, nome ) VALUES ( 1, 'BANANA' );");
c.execute("INSERT INTO tb_fruta ( id, nome ) VALUES ( 2, 'LARANJA' );");
c.execute("INSERT INTO tb_fruta ( id, nome ) VALUES ( 3, 'MELANCIA' );");
c.execute("INSERT INTO tb_fruta ( id, nome ) VALUES ( 4, 'MACA' );");
c.execute("INSERT INTO tb_fruta ( id, nome ) VALUES ( 5, 'UVA' );");
c.execute("INSERT INTO tb_fruta ( id, nome ) VALUES ( 6, 'MORANGO' );");
c.execute("UPDATE tb_fruta SET nome = 'LIMAO' WHERE id = 2;");
c.execute("UPDATE tb_fruta SET nome = 'ABACAXI' WHERE id = 6;");
c.execute("DELETE FROM tb_fruta WHERE id = 3;");
c.execute("DELETE FROM tb_fruta WHERE nome = 'UVA';");
conn.commit()
conn.close()
Testing:
$ sqlite3 foobar.db
SQLite version 3.7.17 2013-05-20 00:56:22
Enter ".help" for instructions
Enter SQL statements terminated with a ";"
sqlite> SELECT * FROM tb_fruta;
1|BANANA
2|LIMAO
4|MACA
6|ABACAXI
sqlite>
Browser other questions tagged python sqlite sqlite3 pycharm
You are not signed in. Login or sign up in order to post.
Start by studying the official documentation of
sqlite3
.– Woss