2015-06-17 71 views
0

我是超新的Python,所以原諒我缺乏知識哈哈,但由於某種原因,我無法讓Python在我的數據庫中插入行。這是我有:使用Python,Tweepy,插入光標和Arcpy

import sys, arcpy, datetime, tweepy 


consumer_key = " " 
consumer_secret = " " 
access_token = " " 
access_token_secret = " " 

auth = tweepy.OAuthHandler(consumer_key, consumer_secret) 
auth.set_access_token(access_token, access_token_secret) 
api = tweepy.API(auth) 

table = r"C:\....dbf" 

rows = arcpy.InsertCursor(table) 

class CustomStreamListener(tweepy.StreamListener): 
    def on_status(self, status): 
     try: 


      user = status.user.screen_name 
      tweet = status.text 
      coord_x = status.coordinates['coordinates'][0] 
      coord_y = status.coordinates['coordinates'][1] 
      date_utc = status.created_at 
      h_m_s_utc = (str(status.created_at.hour))+':'+(str(status.created_at.minute))+':'+(str(status.created_at.second)) 
      date_est = datetime.datetime.now() 
      h_m_s_est = (str(date_est.hour))+':'+(str(date_est.minute))+':'+(str(date_est.second)) 


      row.user_name=user 
      row.tweet=tweet 
      row.coord_x=coord_x 
      row.coord_y=coord_y 
      row.date_utc=date_utc 
      row.h_m_s_utc=h_m_s_utc 
      row.date_est=date_est 
      rows.insertRow(row) 
      del row, rows 
      insert_table= r"C:\....dbf" 
      insert_row(insert_table) 

     print user 
     print tweet 

    except: 
     # If there are no coordinates for a tweet, then pass 
     pass 

def on_error(self, status_code): 
    print >> sys.stderr, 'Encountered error with status code:', status_code 
    return True # Don't kill the stream 

def on_timeout(self): 
    print >> sys.stderr, 'Timeout...' 
    return True # Don't kill the stream 

# ----------------Script execution---------------- 
listener = tweepy.streaming.Stream(auth, CustomStreamListener()) 
listener.filter(track=[' love ', '#love']) 

我很確定它與row.rowID的事情有關。

對不起,如果這是一場災難!任何幫助深表感謝!

+0

燦你發佈了一個回溯你的錯誤? – Kevin

回答

0

我看起來像你忘記調用插入光標的數據訪問(.da)方法。

with arcpy.da.InsertCursor(in_table, field_names) as inCursor: 
    for row in rows: 
     inCursor.insertRow(row) # example 

- 或 -

inCursor = arcpy.da.InsertCursor(in_table, field_names) 
for row in rows: 
    cursor.insertRow(row) # example 
del inCursor # make sure to delete cursor if you do it this way as to avoid data lock. 

此外,如果你只是想插入光標方法,您可以

from arcpy import da 

欲瞭解更多信息,請訪問: http://resources.arcgis.com/en/help/main/10.2/index.html#//018w0000000t000000