0
#function used to get tickers from the tweets
def getTicker():
 for tweet in tweets:
 if "$" in tweet.text:
 x = tweet.text.split()
 for i in x:
 if i.startswith("$") and i[1].isalpha():
 tickList.append(i)
#running the ticker function
getTicker()
print(tickList)
 
#connecting to the database
conn = pyodbc.connect(
 "Driver={SQL Server};"
 "Server=SERVERNAME;"
 "Database=DBNAME;"
 "Trusted_Connection=yes;")
#query to put the tickers into the
cursor = conn.cursor()
# print(var_string)
cursor.execute('INSERT INTO master.dbo.TickerTable (TickerName) VALUES (?);', [','.join(tickList)])
conn.commit()

This inserts a list of tickers into the table but they all go in as one item instead of individually. Is there a way that I can take the list and insert the one by one?

marc_s
760k186 gold badges1.4k silver badges1.5k bronze badges
asked Oct 23, 2020 at 20:22
1
  • ....use a loop? Commented Oct 23, 2020 at 20:25

2 Answers 2

1

You are plugging the entire joined string value inside the parametrized query: VALUES (?);. You can either retain the same logic and introduce a loop which executes one SQL operation for each ticker, or you could adjust your string formatting to include wrapping parentheses to VALUES ?; per se:

INSERT INTO master.dbo.TickerTable (TickerName) VALUES (ticker_1),(ticker_2),(ticker_3);
answered Oct 23, 2020 at 20:38
Sign up to request clarification or add additional context in comments.

Comments

0

For the item in the tickList :

cursor.execute('INSERT INTO master.dbo.TickerTable (TickerName) VALUES (?);', item)
ucMax
5,5715 gold badges52 silver badges57 bronze badges
answered Oct 23, 2020 at 20:35

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.