sqlite , python c.execute AttributeError: 'str' object has no attribute 'execute' - python

Getting an error:
c.execute('CREATE TABLE IF NOT EXISTS top15' +today +'(symbol TEXT, ending_price REAl, volume REAL, percent REAL)')
AttributeError: 'str' object has no attribute 'execute'
Below is original code where no error occurs.
conn = sqlite3.connect('Top15.db')
c = conn.cursor()
def create_table():
c.execute('CREATE TABLE IF NOT EXISTS top15' +today +'(symbol TEXT, ending_price REAl, volume REAL, percent REAL)')
f15= ['hgd', 'bbd.a', 'mri.u', 'iam', 'hnd', 'tth', 'bbd.b', 'bbd.pr.c', 'esp', 'enl', 'rmp', 'amm', 'vrx', 'xtc', 'cxr']
f45=['4,433,389', '2.96', '-13.7', '1,209,421', '2.25', '-13.1', '3,000', '8.60', '-8.5', '1,000', '1.06', '-7.8', '1,180,466', '21.76', '-7.6', '41,777', '0.97', '-7.6', '32,423,597', '1.89', '-7.4', '43,737', '15.20', '-7.3', '87,604', '1.96', '-7.1', '5,239', '34.00', '-6.2', '2,688,261', '1.83', '-5.7', '63,301', '1.39', '-5.4', '1,664,689', '41.83', '-5.4', '63,453', '13.45', '-5.3', '1,642,197', '36.48', '-5.0']
def dynamic_data_entry():
volume = first_45[i]
ending_price = first_45[i+1]
percent = first_45[i+2]
symbol = first_15[z]
c.execute("INSERT INTO top15" +today +"(symbol,ending_price, volume, percent) VALUES (?, ?, ?, ?)",
(symbol,ending_price, volume, percent))
conn.commit()
create_table()
for i, z in zip(range(0,45,3),range(15)):
dynamic_data_entry()
c.close
conn.close
Below is the new setup. Nothing else has changed other than turning the two lists (f15,f45) into a single list (result) with internal lists. However,now I get the c.execute error. Ive read about c.execute errors and cannot find a solution
conn = sqlite3.connect('Top15.db')
c = conn.cursor()
def create_table():
c.execute('CREATE TABLE IF NOT EXISTS top15' +today +'(symbol TEXT, ending_price REAl, volume REAL, percent REAL)')
f15=[15 list items]
f45=[45 list items]
f45i = iter(f45)
result = [[a, c, b, d] for (a, b, c, d) in zip(f15, f45i, f45i, f45i)]
result = filter(lambda l: l[0].count('.') <= 1, result)
print result
result=[['hgd', '2.96', '4,433,389', '-13.7'], ['bbd.a', '2.25', '1,209,421', '-13.1'], ['mri.u', '8.60', '3,000', '-8.5'], ['iam', '1.06', '1,000', '-7.8'], ['hnd', '21.76', '1,180,466', '-7.6'], ['tth', '0.97', '41,777', '-7.6'], ['bbd.b', '1.89', '32,423,597', '-7.4'], ['esp', '1.96', '87,604', '-7.1'], ['enl', '34.00', '5,239', '-6.2'], ['rmp', '1.83', '2,688,261', '-5.7'], ['amm', '1.39', '63,301', '-5.4'], ['vrx', '41.83', '1,664,689', '-5.4'], ['xtc', '13.45', '63,453', '-5.3'], ['cxr', '36.48', '1,642,197', '-5.0']]
def dynamic_data_entry():
symbol = result[i][0]
ending_price = result[i][1]
volume = result[i][2]
percent = result[i][3]
c.execute("INSERT INTO top15" +today +"(symbol, ending_price, volume, percent) VALUES (?, ?, ?, ?)",
(symbol,ending_price, volume, percent))
conn.commit()
create_table()
i=0
while i <len(result):
dynamic_data_entry()
i+=1
c.close
conn.close
Is there something about the list inside the list creating the problem? Not too sure why this would now result in this error. When it worked perfectly fine below

Related

ib_insync, prints ticker even though I have not specified it

Just started using ib_insync. I am trying to get the tick data into a dataframe.
Here is the relevant code:
def onPendingTickers(tickers, conn=conn):
for t in tickers:
# 'CREATE TABLE IF NOT EXISTS {} (timestamp timestamp, bid_qty INT, bid REAL, ask REAL, ' \
# 'ask_qty INT, high REAL, low REAL, close REAL, open REAL, contractID INT)'
# print(t)
c.execute('INSERT INTO {} (timestamp, bid_qty, bid, ask, ask_qty, high, low, close, open, contractID)'
' VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?);'.format(t.contract.pair()),
(t.time, t.bidSize, t.bid, t.ask, t.askSize, t.high, t.low, t.close, t.open, t.contract.conId))
# print(t.time, t.bidSize, t.bid, t.ask, t.askSize, t.high, t.low, t.close, t.open, t.contract.conId)
conn.commit()
ib.pendingTickersEvent += onPendingTickers
ib.sleep(60*60)
ib.pendingTickersEvent -= onPendingTickers
When I run this code in a terminal, it prints the ticker, I am not sure what exactly needs to be changed here.
If you just want to get ticks without displaying the information, here's some sample code that you should be able to run:
from ib_insync import *
import pandas as pd
import numpy as np
# Connect to IB; args are (IP address, device number, client ID)
def ibConnect(port,clientID):
connection = ib.connect('127.0.0.1', port, clientID)
ib.sleep(0)
return ()
# Disconnect from IB
def ibDisconnect():
ib.disconnect()
ib.sleep(0)
return
# Set up a futures contract
def ibFuturesContract(symbol, expirationDate, exchange):
futuresContract = Future(symbol, expirationDate, exchange)
return futuresContract
# Realtime Ticks Subscription
def ibGetTicker (contract):
ticker = ib.ticker(contract)
return [ticker]
ib = IB()
ibConnect(7496,300)
contract = ibFuturesContract('YM',20210618,'ECBOT')
# Start the real-time tick subscription
ib.reqMktData(contract, '', False, False)
# Real Time Ticks
global ticker
ticker = ibGetTicker(contract)
# Get just the last tick each second and put it into a data table
x = 0
while x < 10:
ib.sleep(1)
if ticker is not None:
df = util.df(ticker)
if (x == 0):
dt = df
else:
dt = dt.append(df)
x = x + 1
print (dt)
ib.cancelMktData(contract)
ibDisconnect()

Python Mysql Insert - tuple index out of range

Here is the snippet of code I am having problem with
for x in myresult:
sql = "INSERT INTO `writing_correction` (`autoinc`, `q_usage_id`, `ruleId`, `message`, `replacements`, `offset`, `errorLength`, `category`, `ruleIssueType`) VALUES (NULL, %d, %s, %s, %s, %d, %d, %s, %s )"
# sql = "INSERT INTO customers (name, address) VALUES (%s, %s)"
# sql = "INSERT INTO `writing_correction` (`autoinc`, `q_usage_id`, `ruleId`, `message`, `replacements`, `offset`, `errorLength`, `category`, `ruleIssueType`) VALUES (NULL, '100', 'ruleid', 'message', 'replacemenet', '12', '3', 'cat', 'ruleissuetype')"
# val = ("John", "Highway 21")
# mycursor.execute(sql, val)
print(x[3])
matches = tool.check(x[3])
for y in matches:
# sql = "INSERT INTO `writing_correction` (`autoinc`, `q_usage_id`, `ruleId`, `message`, `replacements`, `offset`, `errorLength`, `category`, `ruleIssueType`) VALUES (NULL, %d, %s, %s, %s, %d, %d, %s, %s )" % ( x[0], y.ruleId, y.message, y.replacements, y.offset , y.errorLength, y.category, y.ruleIssueType )
val = ( [ x[0] ], (y.ruleId), (y.message), (y.replacements), [y.offset] , [y.errorLength] , (y.category), (y.ruleIssueType) )
print(val)
# mycursor.execute(sql , ( x[0], y.ruleId, y.message, y.replacements, y.offset , y.errorLength, y.category, y.ruleIssueType ) )
mycursor.executemany(sql, val)
the commented code is my trial and error attempt to make it work but it is not working for some reason.
Currently I am getting following error:
File "/usr/local/lib/python3.8/dist-packages/mysql/connector/cursor.py", line 75, in __call__
return bytes(self.params[index])
IndexError: tuple index out of range
val should be an array of tuples. With each tuple corresponding to a row. So fill out the array at a time, and then executemany once.
so:
val = []
for y in matches:
val.append( ( x[0], y.ruleId, y.message, y.replacements, y.offset , y.errorLength , y.category, y.ruleIssueType ) )
print(val)
mycursor.executemany(sql, val)
ref: executemany

SQLAlchemy not execute a query

I'm having a problem with sqlAlchemy when i try to execute a query. My script has been working fine and every query.execute worked good until now. Here is the code:
for i in listaUnificacion:
usu = "'AUTO'"
incabuniper = "'S'"
sCodPersonaPr, sPers = i[0], i[1]
engine = sqla.create_engine(URL_ORACLE)
connection = engine.connect()
seq_query = sqla.Sequence('SEQ_PERUNI')
pnCodSecPerUni = connection.execute(seq_query)
query = "INSERT INTO TABLE1(SEC, CD, CDUNIF, DATE, USU, INCABUNIPER) VALUES({0}, {1}, {2}, SYSDATE, {3}, {4})".format(pnCodSecPerUni, sCodPersonaPr, sPers, str(usu), str(incabuniper))
query = sqla.text(query)
print(query)
connection.execute(query)
query = "UPDATE TABLE2 SET type = 'M' WHERE cd = {}".format(sPers);
connection.execute(query)
query_uni = "DECLARE\
res varchar2(100);\
errorm varchar2(1000);\
BEGIN\
res := USER.FNC({0},{1},{2},'AUTO',errorm);\
END;".format(pnCodSecPerUni, sCodPersonaPr, sPers)
query_uni = sqla.text(query_unifica)
connection.execute(query_uni)
connection.close()
When I try to execute query_unifica, it doesn't work but it doesn't show any error. I put here the execution with some prints:
PARES
(11005202, 11002071)
INSERT INTO TABLE1(SEC, CD, CDUNIF,, DATE, USU, INCABUNIPER)
VALUES(1628226, 11005202, 11002071, SYSDATE, 'AUTO', 'S') --> WORKS FINE
UPDATE TABLE2 SET type = 'M' WHERE cd = 11002071 --> works fine
DECLARE res varchar2(100); errorm
varchar2(1000); BEGIN res :=
USER.FNC(1628226,11005202,11002071,'AUTO',errorm); END; --
> DOSEN'T WORK!!!

SQL insert into.. where (python)

I have the following code:
def create_table():
c.execute('CREATE TABLE IF NOT EXISTS TEST(SITE TEXT, SPORT TEXT, TOURNAMENT TEXT, TEAM_1 TEXT, TEAM_2 TEXT, DOUBLE_CHANCE_1X TEXT, DOUBLE_CHANCE_X2 TEXT, DOUBLE_CHANCE_12 TEXT, DRAW_1 TEXT, DRAW_2 TEXT DATE_ODDS TEXT, TIME_ODDS TEXT)')
create_table()
def data_entry():
c.execute("INSERT INTO TEST(SITE, SPORT, TOURNAMENT, TEAM_1, TEAM_2, DOUBLE_CHANCE_1X, DOUBLE_CHANCE_X2, DOUBLE_CHANCE_12, DATE_ODDS, TIME_ODDS) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
(Site, sport.strip(), tournament.strip(), team_1.strip(), team_2.strip(), x_odd.strip(), y_odd.strip(), z_odd.strip(), Date_odds, Time_odds))
conn.commit()
def double_chance():
c.execute("UPDATE TEST SET DOUBLE_CHANCE_1X = x_odd, DOUBLE_CHANCE_X2 = y_odd, DOUBLE_CHANCE_12 = z_odd WHERE TOURNAMENT = tournament and TEAM_1 = team_1 and TEAM_2 = team_2 and DATE_ODDS = Date_odds and TIME_ODDS = Time_odds")
conn.commit()
driver.get(link)
Date_odds = time.strftime('%Y-%m-%d')
Time_odds = time.strftime('%H:%M')
sport = (driver.find_element_by_xpath(".//*[#id='breadcrumb']/li[2]/a")).text #example Footbal
tournament = (driver.find_element_by_xpath(".//*[#id='breadcrumb']/li[4]/a")).text #example Premier League
try:
div = (driver.find_element_by_xpath(".//*[#id='breadcrumb']/li[5]/a")).text #to find any division if exists
except NoSuchElementException:
div = ""
market = driver.find_element_by_xpath(".//*[contains(#id,'ip_market_name_')]")
market_name = market.text
market_num = market.get_attribute('id')[-9:]
print market_num
team_1 = (driver.find_element_by_xpath(".//*[#id='ip_marketBody" + market_num + "']/tr/td[1]//*[contains(#id,'name')]")).text
team_2 = (driver.find_element_by_xpath(".//*[#id='ip_marketBody" + market_num + "']/tr/td[3]//*[contains(#id,'name')]")).text
print sport, tournament, market_name, team_1, team_2
data_entry() #first SQL call
for ip in driver.find_elements_by_xpath(".//*[contains(#id,'ip_market3')]"):
num = ip.get_attribute('id')[-9:]
type = (driver.find_element_by_xpath(".//*[contains(#id,'ip_market_name_" + num + "')]")).text
if type == 'Double Chance':
print type
print num
x_odd = (driver.find_element_by_xpath(".//*[#id='ip_market" + num + "']/table/tbody/tr/td[1]//*[contains(#id,'price')]")).text
y_odd = (driver.find_element_by_xpath(".//*[#id='ip_market" + num + "']/table/tbody/tr/td[2]//*[contains(#id,'price')]")).text
z_odd = (driver.find_element_by_xpath(".//*[#id='ip_market" + num + "']/table/tbody/tr/td[3]//*[contains(#id,'price')]")).text
print x_odd, y_odd, z_odd
double_chance() #second SQL call
c.close()
conn.close()
Update:
Based on the answer below I updated the code, but I can't make it work.
When I run it, I get the following error:
sqlite3.OperationalError: no such column: x_odd
What should I do?
Update 2:
I found the solution:
I created an unique ID in order to be able to select exactly the row I want when I run the second SQL query. In this case it doesn't modify any other rows:
def double_chance():
c.execute("UPDATE TEST SET DOUBLE_CHANCE_1X = (?), DOUBLE_CHANCE_X2 = (?), DOUBLE_CHANCE_12 = (?) WHERE ID = (?)",(x_odd, y_odd, z_odd, ID_unique))
conn.commit()
Now it works perfectly.
Use the UPDATE statement to update columns in an existing row.
UPDATE TEST SET DRAW_1=value1,DRAW_2=value2 WHERE column3=value3;
If data_entry(1) is always called first, then change the statement in data_entry_2() to UPDATE. If not you will need to check if the row exists in both cases and INSERT or UPDATE accordingly.

Data Not Being Inserted Into Sqlite3 Database Using Python 2.7

I'm having trouble inserting data into my table. I have a list of stocks that I pass to the function getStockData.
I use a for loop to iterate through the list and get the data for each ticker symbol. At the end I put all the information into a dictionary. My final step is to insert the data into a table. I've been unsuccessful at inserting the data in the dictionary into my table.
def getStockData(x):
nowdate = raw_input("What Is Todays Date?: ")
print "Todays list has %d stocks on it\n" % len(x)
for stock in x:
stockPrice = ystockquote.get_price(stock)
stockPriceChange = ystockquote.get_change(stock)
originalPrice = float(stockPrice) + (float(stockPriceChange) * -1)
changePercentage = (float(stockPriceChange) / originalPrice) * 100
stockDict = {'Date': nowdate, 'Ticker Symbol': stock, 'Closing Price': stockPrice,
'Price Change': stockPriceChange, 'Percentage Changed': changePercentage}
conn = db.connect('stocks.db')
cursor = conn.cursor()
cursor.execute('insert into losers values (?, ?, ?, ?, ?)', (stockDict['Date'], stockDict['Ticker Symbol'], stockDict['Price Change'],
stockDict['Percentage Changed'], stockDict['Closing Price']) )
conn.close()
I think you forget to commit your data to your DB before close.
Try
conn.commit()

Categories

Resources