1

I am writing a function which stores tweets pulled with tweepy in Python into a database(SQL server). I have written the following code to insert into the SQL DB

 conn = pymssql.connect(host,user,passwd,DB)
cursor = conn.cursor()


def insertTweet(user,text,image):
    query = "INSERT INTO [TWEETS].[dbo].[TWEET_INFO] ] 
    (username,text,image_url,approved)\
    values (user, text, image, 0)"
    cursor.execute(query)

This is a very simple function which shouldn't really give an error, but I do get one saying that text is an invalid column name. :

Traceback (most recent call last):
File "db_connect.py", line 35, in <module>
insertTweet(user,text,image)
File "db_connect.py", line 19, in insertTweet
cursor.execute(query)
File "src\pymssql.pyx", line 465, in pymssql.Cursor.execute
pymssql.ProgrammingError: (207, b"Invalid column name 'text'.DB-Lib error 
messag
e 20018, severity 16:\nGeneral SQL Server error: Check messages from the SQL 
Ser
ver\nDB-Lib error message 20018, severity 16:\nGeneral SQL Server error: 
Check m
essages from the SQL Server\n")

Following is the DB structure :

SELECT TOP 1000 [Id]
  ,[username]
  ,[text]
  ,[image_url]
  ,[approved]
  FROM [TWEETS].[dbo].[TWEET_INFO]

I am not sure how to figure this out. I am able to add by manually running a query in sql studio

1 Answer 1

2

Try doing it this way,
Assuming that conn is your connection to the db

query = ("INSERT INTO  billed_items(item_name,billed_qty,price,item_bill_series) VALUES(%s,%s,%s,%s)")
c.execute(query,((name),(no),(price),(series))
conn.commit()
Sign up to request clarification or add additional context in comments.

2 Comments

that worked! I am not sure why though. I have been using the format I mentioned for a while now.
Sorry, the above is not useful.

Your Answer

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.