Why SQLite and how to download?
|
import sqlite3#If not already installed go to terminal or Command prompt and type: pip3 install sqlite3 connection = sqlite3.connect("sports.db")#Create a new database or connect to an existing database cursor = connection.cursor()#The cursor simulates python using your cursor in sqlite #The next block of code creates a table and fields with data types - not needed if the table is already create cursor.execute("""CREATE TABLE game_details ( gameID PRIMARY KEY, gameName text, gameDeveloper text)""") cursor.execute("INSERT INTO game_details VALUES 'G001', 'Tetris','Pajitnov')")#Inserting a single item of new data into the table connection.commit()#Action the changes to the database
cursor.execute("""CREATE TABLE IF NOT EXISTS game_details ( gameID PRIMARY KEY, gameName text, gameDeveloper text)""")
import sqlite3 sports = sqlite3.connect("eSports.db") cursor = sports.cursor() gameList = ("G008", "Tetris","Pajitnov","G009","Donkey Kong","Rare") tupleLen = ((len(gameList))/3)#3 fields per record so dividing the list by 3 gives the number of loops needed record = 0# Used to represent each record starting with G006 then G007 in this example for i in range (int(tupleLen)): sports.execute("INSERT INTO game_details VALUES ('{}','{}','{}')".format(gameList[record],gameList[record+1],gameList[record+2])) record = record + 3 #Shifts from the first record to the next because each record has 3 elements sports.commit() sports.close()
import sqlite3 sports = sqlite3.connect("eSports.db") cursor = sports.cursor() gameList = ["G006", "Tetris","Pajitnov","G007","Donkey Kong","Rare"] listLen = ((len(gameList))/3)#3 fields per record so dividing the list by 3 gives the number of loops needed record = 0# Used to represent each record starting with G006 then G007 in this example for i in range (int(listLen)): sports.execute("INSERT INTO game_details VALUES ('{}','{}','{}')".format(gameList[record],gameList[record+1],gameList[record+2])) record = record + 3 #Shifts from the first record to the next because each record has 3 elements sports.commit() sports.close()
import sqlite3 sports = sqlite3.connect("eSports.db") cursor = sports.cursor() gameList = [["G001", "Tetris","Pajitnov"],["G002","Donkey Kong","Rare"]] for i in range (len(gameList)): sports.execute("INSERT INTO game_details VALUES ('{}','{}','{}')".format(gameList[i][0],gameList[i][1],gameList[i][2])) sports.commit() sports.close()
import sqlite3 connection = sqlite3.connect("sports.db") cursor = connection.cursor() #Allow the user to choose how many items of data to add then add them to the table noOfGames = int(input("How many games would you like to add to the database?")) for i in range (noOfGames):#Loop and ask the user for the detail to add gID = input("Please enter the game ID for Game " + str (i+1) + ":")#Remember this is a PK so need to check if unique gName = input("Please enter the game Name for Game " + str (i+1) + ":")#Remember the input needs to be checked if they are a valid datatype matching the db setup gDeveloper = input("Please enter the game Developer for Game " + str (i+1) + ":") cursor.execute("INSERT INTO game_details VALUES ('{}', '{}','{}')".format(gID,gName,gDeveloper))#Use the .format method to insert data from variables connection.commit()
import sqlite3 connection = sqlite3.connect("sports.db") cursor = connection.cursor() #Selecting all of the data from a specified table cursor.execute("SELECT * FROM game_details") myData = (cursor.fetchall())#This line fetches the data that was requested on the line above print (myData)#If you print you will notice this is a tuple #To change the tuple to a 2D list myList = [] for i in range (len(myData)): myList.append(list(myData[i]))#Create a 2 dimensional list from the data fetched from your database print (myList)#You will now see this is a 2D list and you can now easily manipulate the list in Python connection.commit() connection.close()
import sqlite3 connection = sqlite3.connect("sports.db") cursor = connection.cursor() cursor.execute("UPDATE game_details SET gameDeveloper = 'Pajitnov' WHERE gameID = 'G001'") connection.commit() connection.close()
import sqlite3 connection = sqlite3.connect("sports.db") cursor = connection.cursor() cursor.execute("DELETE * FROM game_details WHERE gameID = 'G001'") connection.commit() connection.close()
SUGGESTIONS
We would love to hear from you |
SUBSCRIBE
To enjoy more benefits |