cursor.execute(''' INSERT INTO profile (name,birth,color,foods,cats) VALUES(%s,%s,%s,%s,%s) ''', ("De'Mont", "1973-01-12", None, "eggroll", 4)) # can also do "conn.literal(data) for every field # testing for NULL values; pretty simple: # not really needed; Pythong knows it's None.. cursor = conn.cursor() cursor.execute("SELECT name, birth, foods FROM profile") for row in cursor.fetchall(): row = list(row) # bc shit comes in tuples, yo for i,e in enumerate(row): if not e: # is this shit empty? row[i] = "NULL" '''do stuff w/ row''' cursor.close() # fun stuff like this... conn = connect.handcon(db='cookbook',p='5ucky*ma') cursor = conn.cursor() keep = [] for i in range(1,67): cursor.execute('''SELECT COUNT(*) FROM kjv WHERE MATCH(vtext) AGAINST('Satan') AND bnum<=%d'''\ % (i)) data = list(cursor.fetchall()) keep.append(int(list(data[0])[0]))
cursor = conn.cursor() cursor.execute( ''' INSERT INTO profile (name,birth,color,foods,cats) VALUES(%s,%s,%s,%s,%s) ''', ("De'Mont", "1973-01-12", None, "eggroll", 4)) # can also do "conn.literal(data) for every field # testing for NULL values; pretty simple: # not really needed; Pythong knows it's None.. cursor = conn.cursor() cursor.execute("SELECT name, birth, foods FROM profile") for row in cursor.fetchall(): row = list(row) # bc shit comes in tuples, yo for i, e in enumerate(row): if not e: # is this shit empty? row[i] = "NULL" '''do stuff w/ row''' cursor.close() # fun stuff like this... conn = connect.handcon(db='cookbook', p='5ucky*ma') cursor = conn.cursor() keep = [] for i in range(1, 67): cursor.execute('''SELECT COUNT(*) FROM kjv WHERE MATCH(vtext) AGAINST('Satan') AND bnum<=%d'''\ % (i)) data = list(cursor.fetchall()) keep.append(int(list(data[0])[0]))
new_date = year + '-' + mont + '-' + days + ' ' +\ hour + ':' + mins + ':' + secs else: new_date = None return new_date def setNulls(d): for key in d.keys(): if not d[key]: d[key] = None return d if __name__=="__main__": data_file, db, p, table = sys.argv[1:] conn = connect.handcon(db=db, p=p) if conn: BLOG_DATA = unpickledata(data_file) cursor = conn.cursor() count = 0 for d in BLOG_DATA: if count%50==0: print count d = setNulls(d) print '''INSERT INTO BasicStoryMeta (Title, DateTime, URL, Source, Author, Keywords, Description, Attrs) VALUES (%s,%s,%s,%s,%s,%s,%s,%s)''' % \ (d['title'], formSQLDateTime(d['date']), d['link'], d['source'], d['author'], d['tags'], d['summary'],