Al fin después de mucho luchar hemos dado con el resultado de este dilema que algunos lectores de este sitio web presentan. Si quieres aportar algún detalle no dejes de dejar tu conocimiento.
Ejemplo 1: insertar registros en la tabla sqlite python
import sqlite3
def insertMultipleRecords(recordList):
try:
sqliteConnection = sqlite3.connect('SQLite_Python.db')cursor= sqliteConnection.cursor()print("Connected to SQLite")
sqlite_insert_query ="""INSERT INTO SqliteDb_developers
(id, name, email, joining_date, salary)
VALUES (?, ?, ?, ?, ?);"""cursor.executemany(sqlite_insert_query, recordList)
sqliteConnection.commit()print("Total",cursor.rowcount,"Records inserted successfully into SqliteDb_developers table")
sqliteConnection.commit()cursor.close()except sqlite3.Error as error:
print("Failed to insert multiple records into sqlite table", error)
finally:
if(sqliteConnection):
sqliteConnection.close()print("The SQLite connection is closed")
recordsToInsert =[(4,'Jos','[email protected]','2019-01-14',9500),(5,'Chris','[email protected]','2019-05-15',7600),(6,'Jonny','[email protected]','2019-03-27',8400)]
insertMultipleRecords(recordsToInsert)
Ejemplo 2: insertar en sqlite python
import sqlite3
def insertVaribleIntoTable(id, name, email, joinDate, salary):
try:
sqliteConnection = sqlite3.connect('SQLite_Python.db')cursor= sqliteConnection.cursor()print("Connected to SQLite")
sqlite_insert_with_param ="""INSERT INTO SqliteDb_developers
(id, name, email, joining_date, salary)
VALUES (?, ?, ?, ?, ?);"""
data_tuple =(id, name, email, joinDate, salary)cursor.execute(sqlite_insert_with_param, data_tuple)
sqliteConnection.commit()print("Python Variables inserted successfully into SqliteDb_developers table")cursor.close()except sqlite3.Error as error:
print("Failed to insert Python variable into sqlite table", error)
finally:
if(sqliteConnection):
sqliteConnection.close()print("The SQLite connection is closed")
insertVaribleIntoTable(2,'Joe','[email protected]','2019-05-19',9000)
insertVaribleIntoTable(3,'Ben','[email protected]','2019-02-23',9500)
Si haces scroll puedes encontrar las acotaciones de otros programadores, tú además puedes dejar el tuyo si te apetece.
¡Haz clic para puntuar esta entrada!
(Votos: 0 Promedio: 0)