Get API-endpoint and store it in a SQLite (Python) - python

As you can see I am trying to fetch data from this API-endpoint https://api.coindesk.com/v1/bpi/currentprice.json and I have chosen few data I want to fetch and store it in SQLite.
When I try to save it in a database it gives me this error:
Traceback (most recent call last):
File "bitcoin.py", line 41, in <module>
cur.execute("INSERT INTO COINS (Identifier, symbol, description) VALUES (?, ?, ?);", to_db)
sqlite3.ProgrammingError: Binding 1 has no name, but you supplied a dictionary (which has only names).
How can I store the some of the data from API-endpoint into the database?
I'm doing this to learn programming and still new to this so hopefully, you can guide me in the right way.
Here is what I have tried so far:
import requests
import sqlite3
con = sqlite3.connect("COINS.db")
cur = con.cursor()
cur.execute('DROP TABLE IF EXISTS COINS')
cur.execute(
"CREATE TABLE COINS (Identifier INTEGER PRIMARY KEY, symbol TEXT, description TEXT);"
)
r = requests.get('https://api.coindesk.com/v1/bpi/currentprice.json')
to_db = r.json() # I do not have to do it in json, CSV would also be another
# solution but the data that is been stored cannot be static.
# It has to automatically fetch the data from API-endpoint
cur.execute("INSERT INTO COINS (Identifier, symbol, description) VALUES (?, ?, ?);", to_db)
con.commit()
con.close()

import requests
import sqlite3
con = sqlite3.connect("COINS.db")
cur = con.cursor()
cur.execute('DROP TABLE IF EXISTS COINS')
cur.execute(
"CREATE TABLE COINS (id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENTUNIQUE,
symbol TEXT, description TEXT);")
r = requests.get('https://api.coindesk.com/v1/bpi/currentprice.json')
to_db = r.json()
des=to_db['bpi']['USD']['description']
code=to_db['bpi']['USD']['code']
cur.execute("INSERT INTO COINS (symbol, description) VALUES (?, ?);",
(des,code))
con.commit()
con.close()
Check full code

Related

"Incorrect number of bindings supplied" errors

Attempting to build a database, inserting from 33 CSV files, my insert statement returns:
ProgrammingError: Incorrect number of bindings supplied. The current statement uses 4, and there are 11 supplied.
I have a CSV file I need to convert a single column from into a string then insert that CSV file into a table. How to do this? I want to create a database I can run queries on after inserting CSV file data into its tables.
import sqlite3
import pandas as pd
from traceback import print_exc as pe
import csv
def Connect():
conn = sqlite3.connect('storefront.db')
curs = conn.cursor()
curs.execute("PRAGMA foreign_keys = ON;") #change back to on after finding errors
return conn, curs
def BuildTables():
conn, curs = Connect()
curs.execute("DROP TABLE IF EXISTS tState;")
curs.execute("DROP TABLE IF EXISTS tZip;")
curs.execute("DROP TABLE IF EXISTS tCust;")
curs.execute("DROP TABLE IF EXISTS tOrder;")
curs.execute("DROP TABLE IF EXISTS tOrderDetail;")
curs.execute("DROP TABLE IF EXISTS tProd;")
sql = """
CREATE TABLE tState(
st TEXT NOT NULL PRIMARY KEY,
state TEXT NOT NULL
);"""
curs.execute(sql)
file = open('data/states.csv')
contents = csv.reader(file)
insert_records = "INSERT INTO tState (st, state) VALUES(?, ?)"
curs.executemany(insert_records, contents)
sql = """
CREATE TABLE tZip(
zip TEXT NOT NULL PRIMARY KEY,
city TEXT NOT NULL,
st TEXT NOT NULL
);"""
curs.execute(sql)
sql = """
CREATE TABLE tCust(
cust_id int AUTO_INCREMENT PRIMARY KEY,
first TEXT NOT NULL,
last TEXT NOT NULL,
addr TEXT NOT NULL,
zip TEXT NOT NULL
);"""
curs.execute(sql)
file3 = open('data/Sales_201901.csv')
cont = csv.reader(file3)
cust = "INSERT INTO tCust (first, last, addr, zip) VALUES(?, ?, ?, ?)"
curs.executemany(cust, cont)
sql = """
CREATE TABLE tOrder(
order_id int AUTO_INCREMENT PRIMARY KEY,
date TEXT NOT NULL
);"""
curs.execute(sql)
order = "INSERT INTO tOrder (date) VALUES(?)"
curs.executemany(order, cont)
sql = """
CREATE TABLE tOrderDetail(
order_id INTEGER NOT NULL REFERENCES tOrder(order_id),
cust_id INTEGER NOT NULL REFERENCES tCust(cust_id),
qty TEXT,
PRIMARY KEY (order_id, cust_id)
);"""
curs.execute(sql)
orderdetails = "INSERT INTO tOrderDetail (qty) VALUES(?)"
curs.executemany(orderdetails, cont)
sql = """
CREATE TABLE tProd(
prod_id INTEGER NOT NULL PRIMARY KEY,
prod_name TEXT NOT NULL,
unit_price TEXT NOT NULL
);"""
curs.execute(sql)
file4 = open('data/prods.csv')
conts = csv.reader(file4)
prod = "INSERT INTO tProd (prod_id, prod_name, unit_price) VALUES (?, ?, ?)"
curs.executemany(prod, conts)
conn.close()
return True

Pyodbc: how can I get the primary key after a successful insert into the SQL Server database?

I have a table Employee in SQL Server as follows:
ID (AUTO, PK),
firstname (varchar),
lastname (varchar)
I want to insert data like ('John', 'Myers') into the table.
I used the following code in Python using pyodbc:
connection = pyodbc.connect(...)
cursor = connection.cursor()
cursor.execute("insert into employee(firstname, lastname) values(?, ?)", ['John','Myers'])
Is it possible to get the ID value of this newly inserted row without having to write a select query?
You can use the OUTPUT clause
cursor.execute("insert into employee(firstname, lastname) output inserted.ID values(?, ?);", ['John','Myers'])
id = cursor.fetchone()
Alternatively, use SCOPE_IDENTITY()
cursor.execute("insert into employee(firstname, lastname) values(?, ?); select SCOPE_IDENTITY();", ['John','Myers'])
id = cursor.fetchone()

python error: near "From": syntax error

I am trying to insert 6 values into separate columns in a database and when running my code i'm getting the near "From" syntax error can someone help?
def setup_transactions(db, filename):
'''(str, str) -> NoneType
Create and populate the Transactions table for database db using the
contents of the file named filename.'''
data_file = open(filename)
con = sqlite3.connect(db)
cur = con.cursor()
# create and populate the table here
cur.execute('CREATE TABLE Transactions(Date TEXT, Number TEXT, Type TEXT, From TEXT, To TEXT, Amount REAL)')
for line in data_file:
data = line.split()
cur.execute('INSERT INTO Accounts VALUES (?, ?, ?, ?, ?, ?)', (data[0], data[1], data[2], data[3], data[4], data[5]))
data_file.close()
cur.close()
con.commit()
con.close()
the error is this:
Traceback (most recent call last):
Python Shell, prompt 2, line 1
File "/Users/user1/Desktop/assignment 2/banking.py", line 64, in
cur.execute('CREATE TABLE Transactions(Date TEXT, Number TEXT, Type TEXT, From TEXT, To TEXT, Amount REAL)')
sqlite3.OperationalError: near "From": syntax error
cur.execute('CREATE TABLE Transactions(Date TEXT, Number TEXT, Type TEXT, From TEXT, To TEXT, Amount REAL)')
You have a column named From. From is a sql Keyword, I would avoid using it as it may cause syntax errors
Try something more descriptive like
cur.execute('CREATE TABLE Transactions(date_created TEXT, current_Number TEXT, record_type TEXT, from_somewhere TEXT, to_somewhere TEXT, amount REAL)')

Python : Inserting multiple values into a table from excel

I have to read data from Excel and insert it into Table...
For this I am using Python 2.7, pymssql and xlrd modules...
My sql connection is working fine and I am also able to read data from Excel properly.
My table structure :
CREATE TABLE MONTHLY_BUDGET
(
SEQUENCE INT IDENTITY,
TRANSACTION_DATE VARCHAR(100),
TRANSACTION_REMARKS VARCHAR(1000),
WITHDRAWL_AMOUNT VARCHAR(100),
DEPOSIT_AMOUNT VARCHAR(100),
BALANCE_AMOUNT VARCHAR(100)
)
My excel values are like this :
02/01/2015 To RD Ac no 147825000874 7,000.00 - 36,575.74
I am having problem while inserting multiple values in the table... I am not sure how to do this...
import xlrd
import pymssql
file_location = 'C:/Users/praveen/Downloads/OpTransactionHistory03-01-2015.xls'
#Connecting SQL Server
conn = pymssql.connect (host='host',user='user',password='pwd',database='Practice')
cur = conn.cursor()
# Open Workbook
workbook = xlrd.open_workbook(file_location)
# Open Worksheet
sheet = workbook.sheet_by_index(0)
for rows in range(13,sheet.nrows):
for cols in range(sheet.ncols):
cur.execute(
" INSERT INTO MONTHLY_BUDGET VALUES (%s, %s, %s, %s, %s)", <--- Not sure!!!
[(sheet.cell_value(rows,cols))])
conn.commit()
Error :
ValueError: 'params' arg () can be only a tuple or a dictionary.
The docs are here : http://pymssql.org/en/stable/pymssql_examples.html
The exception you are getting says that the "'params' arg() can be only a tuple or a dictionary" but you're passing in a list. Also, your parameter list appears to be a single tuple instead of a list with 4 values. Try changing
cur.execute(
" INSERT INTO MONTHLY_BUDGET VALUES (?, ?, ?, ?, ?)", <--- Not sure!!!
[(sheet.cell_value(rows,cols))])
to
cur.execute(
" INSERT INTO MONTHLY_BUDGET VALUES (?, ?, ?, ?, ?)", <--- Not sure!!!
(sheet.cell_value(rows,cols)))
... or maybe
cur.execute(
" INSERT INTO MONTHLY_BUDGET VALUES (?, ?, ?, ?, ?)", <--- Not sure!!!
((sheet.cell_value(rows,cols))))
NB: untested. I've always changed how the bind variables in your SQL are being called.

Using SQLite3 in Python

I am trying to store some parsed feed contents values in Sqlite database table in python.But facing error.Could anybody help me out of this issue.Infact it is so trivial question to ask!I am newbie!..Anyway thanks in advance!
from sqlite3 import *
import feedparser
data = feedparser.parse("some url")
conn = connect('location.db')
curs = conn.cursor()
curs.execute('''create table location_tr
(id integer primary key, title text ,
updated text)''')
for i in range(len(data['entries'])):
curs.execute("insert into location_tr values\
(NULL, data.entries[i].title,data.feed.updated)")
conn.commit()
curs.execute("select * from location_tr")
for row in curs:
print row
And Error is:
Traceback (most recent call last):
File "F:\JavaWorkspace\Test\src\sqlite_example.py", line 16, in <module>
(NULL, data.entries[i].title,data.feed.updated)")
sqlite3.OperationalError: near "[i]": syntax error
Try
curs.execute("insert into location_tr values\
(NULL, '%s', '%s')" % (data.entries[i].title, data.feed.updated))
the error should be this line
curs.execute("insert into location_tr values\
(NULL, data.entries[i].title,data.feed.updated)")
data.entries[i].title comes from Python. So if you enclose it in double quotes, it becomes a literal string, not a value. It should be something like this:
curs.execute("insert into location_tr values (NULL," + data.entries[i].title +","+data.feed.updated+")")

Categories