Created
April 23, 2015 15:08
-
-
Save avenet/033b959af931a0ea837a to your computer and use it in GitHub Desktop.
SQLite example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sqlite3 | |
created_db = sqlite3.connect('test.db') | |
cursor = created_db.cursor() | |
def create_table(): | |
cursor.execute("""CREATE TABLE IF NOT EXISTS Scores | |
(id INTEGER PRIMARY KEY, Name TEXT, Class TEXT, Score INTERGER)""") | |
def add_pupil(name, klass, score): | |
cursor.execute("""INSERT INTO Scores (Name, Class, Score) | |
VALUES(?,?,?)""",(name, klass, score,)) | |
def main(): | |
create_table() | |
name = "John Doe" | |
klass = "Math" | |
score = 100 | |
add_pupil(name, klass, score) | |
created_db.commit() | |
cursor.execute("SELECT * FROM Scores ORDER BY Score DESC") | |
title = ["id","Name", "Class", "Score"] | |
k = 0 | |
for i in cursor: | |
print "\n" | |
for j in i: | |
print title[k], | |
print j | |
if k < 3: k += 1 | |
else: k = 0 | |
cursor.close() | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment