python sqlite3 insert list - python

I have a python script that should insert a list into a sqlite table. It appears my insert statement is not working.
links = ['a', 'b', 'c']
conn = sqlite3.connect('example.db')
#create a data structure
c = conn.cursor()
#Create table
c.execute('''Create TABLE if not exists server("sites")''')
#Insert links into table
def data_entry():
sites = links
c.execute("INSERT INTO server(sites) VALUES(?)", (sites))
conn.commit()
#query database
c.execute("SELECT * FROM server")
rows = c.fetchall()
for row in rows:
print(row)
conn.close
I checked the database at command line but the "server" table is empty:
C:\App\sqlite\sqlite_databases>sqlite3
SQLite version 3.17.0 2017-02-13 16:02:40
Enter ".help" for usage hints.
Connected to a transient in-memory database.
Use ".open FILENAME" to reopen on a persistent database.
sqlite> .tables
server
sqlite> SELECT * FROM server
...> ;
sqlite>
So it doesn't appear that the list is actually being inserted.

Iterate over list_ and execute INSERT for each item. And call data_entry() to actually insert data.
import sqlite3
list_ = ['a', 'b', 'c']
#create a data structure
conn = sqlite3.connect('example.db')
c = conn.cursor()
#Create table
c.execute('''Create TABLE if not exists server("sites")''')
#Insert links into table
def data_entry():
for item in list_:
c.execute("INSERT INTO server(sites) VALUES(?)", (item,))
conn.commit()
data_entry() # ==> call the function
#query database
c.execute("SELECT * FROM server")
rows = c.fetchall()
for row in rows:
print(row)
conn.close()

Make a 2-dimensional list and use executemany().
links = ['a', 'b', 'c']
conn = sqlite3.connect('example.db')
#create a data structure
c = conn.cursor()
#Create table
c.execute('''Create TABLE if not exists server("sites")''')
#Insert links into table
def data_entry(links):
sites = [(s,) for s in links]
c.executemany("INSERT INTO server(sites) VALUES(?)", sites)
conn.commit()
data_entry(links)
#query database
c.execute("SELECT * FROM server")
rows = c.fetchall()
for row in rows:
print(row)
conn.close

Related

sqlite3 view() prints with empty list?

I have testtable() function that works to create the table if necessary and list all the PDF file names in a column. However, when I execute my view() function, it prints an empty list. Am I missing something or just going about this in the wrong way?
import os, sys
import sqlite3
import csv
testdb = 'pdftestdir.db'
def testtable():
conn = sqlite3.connect(testdb)
cur = conn.cursor()
cur.execute('CREATE TABLE IF NOT EXISTS test (name TEXT)')
path = os.listdir('/root/Desktop/PDF')
conn = sqlite3.connect(testdb)
cur = conn.cursor()
cur.execute('SELECT * FROM test')
exists = cur.fetchall()
for name in path:
if name.endswith('.pdf'):
if not exists:
cur.execute('INSERT INTO test VALUES (?)', (name,))
else:
pass
conn.commit()
conn.close()
def view():
conn = sqlite3.connect(testdb)
cur = conn.cursor()
cur.execute('SELECT * FROM test')
cur.fetchall()
rows = cur.fetchall()
conn.close()
print(rows)
You unnecessarily call cur.fetchall() without storing the returning value to a variable, and the cursor has already reached the end of the rows returned with that call, so the second time you call cur.fetchall() it no longer has any more rows to return.
You can fix this by simply removing the redundant call.
Change:
cur.fetchall()
rows = cur.fetchall()
to:
rows = cur.fetchall()

How to fetch one column data in python pymysql

How to fetch just one column as array in python with pymysql;
for example sql:
select name from users
data:
["Tom", "Ben", "Jon"]
cursor = conn.cursor() # where conn is your connection
cursor.execute('select name from users')
rows = cursor.fetchall()
result_list = [row[0] for row in rows]

python postgres change values of rows while reading results

I am using python3, postgress 10 and Psycopg2 to query multiple records like so
import psycopg2
conn = psycopg2.connect(<my connection string>)
with conn:
with conn.cursor() as cur:
cur.execute('select id,field1 from table1')
for id, field1 from cur.fetchall():
print(id,field1)
#todo: how up update field1 to be f(field1) where f is an arbitrary python function
My question is: how do i update the value of the rows that I am reading and set the value of field1 to some arbitrary python-based calculation
edit: the purpose is to update the rows in the table
You need another cursor, e.g.:
with conn:
with conn.cursor() as cur:
cur.execute('select id,field1 from table1')
for id, field1 in cur.fetchall():
print(id,field1)
with conn.cursor() as cur_update:
cur_update.execute('update table1 set field1 = %s where id = %s', (f(field1), id))
Note however that this involves as many updates as selected rows, which is obviously not efficient. The update can be done in a single query using psycopg2.extras.execute_values():
from psycopg2.extras import execute_values
with conn:
with conn.cursor() as cur:
cur.execute('select id,field1 from table1')
rows = cur.fetchall()
for id, field1 in rows:
print(id,field1)
# convert rows to new values of field1
values = [(id, f(field1)) for id, field1 in rows]
sql = '''
with upd (id, field1) as (values %s)
update table1 t
set field1 = upd.field1
from upd
where upd.id = t.id
'''
with conn.cursor() as cur:
execute_values(cur, sql, values)

Python - INSERT table INTO SQLite from MS SQL Server

I am trying to query MS SQL Server for a table.column, then insert this output into a sqlite table.
This example has one numeric column in the SQL Server source table.
I think I've almost got it by scouring the other answers.
Please let me know what I am missing.
import sqlite3
import pyodbc
#def connect_msss():
ODBC_Prod = ODBC_Prod
SQLSN = SQLSN
SQLpass = SQLpass
conn_str = ('DSN='+ODBC_Prod+';UID='+SQLSN+';PWD='+SQLpass)
conn = pyodbc.connect(conn_str)
#def connect_sqlite():
sl3Conn = sqlite3.connect('server_test.db')
c = sl3Conn.cursor()
c.execute('CREATE TABLE IF NOT EXISTS mrn_test (PTMRN NUMERIC)')
#def query_msss():
cur = conn.cursor()
cur.execute("SELECT TOP 50 PTMRN FROM dbo.atl_1234_mrntest")
rows = cur.fetchall()
for row in rows:
c.execute("INSERT INTO mrn_test VALUES (?)", row)
conn.commit()
#connect_msss()
#connect_sqlite()
#query_msss()
Error 1:
c.execute('CREATE TABLE IF NOT EXISTS mrn_test
(PTMRN NUMERIC)')
Out[117]: <sqlite3.Cursor at 0x2d1a742fc70>
Error 2:
cur = conn.cursor() cur.execute("SELECT TOP 50 PTMRN FROM
dbo.atl_1234_mrntest")
Out[118]: <pyodbc.Cursor at 0x2d1a731b990>
You're not committing the executed changes on the sqlite connection, after the c.execute step you're committing the MySQL DB connection. I think you need to replace conn.commit() at the end with sl3Conn.commit().

Inserting into Postgres DB column with python

conn = psycopg2.connect("dbname=name host=host user=user password=pass port=port")
cur = conn.cursor()
with open('big shot.json') as f:
data = json.load(f)
for key in data["permissions"]:
cur.execute("INSERT INTO permissions (name) VALUES (%s);", (key,))
conn.commit()
output = cur.execute("SELECT * FROM permissions")
print(output)
I have this that I'm trying to use to create new rows in my database, but it doesn't do anything. It doesn't return any errors, but it also doesn't write to my database, and output, obviously, returns "None" in the console.
You need to fetch the data from the cursor:
cur.execute("SELECT * FROM permissions")
data = cur.fetchall()
print(data)

Categories