How can I create a dynamic database in pyhton [closed]

0

Can anyone help me with how to create a dynamic database, where can I add and remove items? In the Python language, if possible with Sqlite

    
asked by anonymous 10.05.2018 / 22:01

1 answer

2

In the example below, the SQLite database is created in the foobar.db file, where a table named tb_fruta is created and its items handled (include, change, and delete) p>

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>
    
11.05.2018 / 01:08