I want to store the Python shell data into a database
Question: I am new to Python. I'm using this code to get the details of a user (in a
Python shell currently).

    
    
    import tweepy
    import time
    import sqlite3
    import os
    from datetime import datetime
    auth = tweepy.OAuthHandler(key,secret)
    auth.set_access_token(access_token, access_secter)
    api = tweepy.API(auth)
    user = api.get_user('<the_user_to_query_on_here>')
    print ("User id: " + user.id_str)
    print ("Screen_Name: " + user.screen_name)
    print (user.name)
    print ("Description: " + user.description)
    print ("Language: " + user.lang)
    print ("Account created at: " + str(user.created_at))
    print ("Location: " + user.location)
    print ("Time zone: " + user.time_zone)
    print ("Number of tweets: " + str(user.statuses_count))
    print ("Number of followers: " + str(user.followers_count))
    print ("Number of Following: " + str(user.friends_count))
    print ("A member of " + str(user.listed_count) + " lists.")
    print ("Retreiving friends for", user.screen_name)
    for friend in user.friends():
        print ("Following_Names: " + str(friend.screen_name))
    

I would like to store this information in a database so that I can retrieve it
locally later. How do I do this?


Answer: You can use [SQLite3](https://docs.python.org/3.4/library/sqlite3.html) that's
already included in Python as a possible solution.

What you'll need to do, instead of calling `print()` to each one, assign it to
a variable, then store that variable into the database.

For example, change:

    
    
    print ("User id: " + user.id_str)
    print ("Screen_Name: " + user.screen_name)
    

to

    
    
    user_id = user.id_str
    screen_same = user.screen_name
    

Then open a connection to the database (before or after as long it doesn't
continously loop) and store those variables using:

    
    
    import sqlite3 as lite
    import sys
    
    con = lite.connect('twitter.db')
    
    with con:
    
        cur = con.cursor()
        # the following line only needed once to create the table
        cur.execute("CREATE TABLE Twitter(User ID, Screen Name, etc...)") 
        cur.execute("INSERT INTO Twitter VALUES(user_id,screen_name, etc...)")
        # or
        # you can insert user.id_str directly without assigning it to a variable
        # cur.execute("INSERT INTO Twitter VALUES(user.id_str,user.screen_name, etc...)")
    

Here's a great [tutorial](http://zetcode.com/db/sqlitepythontutorial/) about
SQLite3

