Arun
Arun

Reputation: 1220

Python MYSQL Insert Issues : not enough arguments for format string

Trying to get the 'expanded_url' & 'time_stamp' from tweets and Inserting the data into MYSQL DB. The insert query seem to be correct , I hope . But there seem to be issue with the query and I keep experiencing issue with "not enough arguments for format string"

Not sure , Where i'm making mistake here. Any suggestions please ?

#! /usr/bin/python


#Description : This script can collect the URLs from Tweets and Records them into research MYSQL DB.

from __future__ import print_function
import tweepy
import json
import MySQLdb
import time
from dateutil import parser

WORDS = ['security']

#CREDENTAILS
CONSUMER_KEY = ""
CONSUMER_SECRET = ""
ACCESS_TOKEN = ""
ACCESS_TOKEN_SECRET = ""

HOST = "192.168.150.94"
USER = "root"
PASSWD = "password"
DATABASE = "twitter"

def store_data(values, insert_time):
    db=MySQLdb.connect(host=HOST, user=USER, passwd=PASSWD, db=DATABASE, charset="utf8")
    cursor = db.cursor()
    cursor.executemany("""INSERT INTO tweet_url VALUES (%s,%s)""",(values,insert_time))
    db.commit()
    cursor.close()
    db.close()
    return

class StreamListener(tweepy.StreamListener):


    def on_connect(self):
        print("We are now connected to the streaming API.")

    def on_error(self, status_code):
        print('An Error has occured: ' + repr(status_code))
        return False

    def on_data(self, data):
        try:
            datajson = json.loads(data)
            #text = datajson['text']
            #screen_name = datajson['user']['screen_name']
            #expanded_url= datajson['entities']['urls'][0]['expanded_url']
            web_url= datajson['entities']['urls']
            print(web_url)
            urls=[]
            for i in web_url:
                urls.append((i['expanded_url']))
            values = [list([item]) for item in urls]
            insert_time=time.strftime('%Y-%m-%d %H:%M:%S')
            store_data(values, insert_time)


        except Exception as e:
                print(e)

auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
auth.set_access_token(ACCESS_TOKEN, ACCESS_TOKEN_SECRET)
listener = StreamListener(api=tweepy.API(wait_on_rate_limit=True))
streamer = tweepy.Stream(auth=auth, listener=listener)
print("Tracking: " + str(WORDS))
streamer.filter(track=WORDS)

Database Details:

Database Name : twitter
Table Name : tweet_urls 
Column in 'tweet_urls' : urls & time_stamp(TYPE:DATETIME) 

Upvotes: 0

Views: 1082

Answers (1)

Xingzhou Liu
Xingzhou Liu

Reputation: 1559

replace:

 cursor.executemany("""INSERT INTO tweet_url VALUES (%s,%s)""",(values,insert_time))

with:

 data = []
 for value in values:
     data.append((value, insert_time))

 # execute many expects tuple (value, insert_time) for every row

 cursor.executemany("""INSERT INTO tweet_url VALUES (%s,%s)""",data)

Upvotes: 1

Related Questions