-
Notifications
You must be signed in to change notification settings - Fork 0
/
setup_sql_table_example.py
50 lines (45 loc) · 1.82 KB
/
setup_sql_table_example.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#sqlite connect and interact with db
#sqlite db name test.sqlite
#sqlite db location c:\python27\
#db tables = users (name)
import sqlite3 #imports sqlite module
conn = sqlite3.connect('test.sqlite') #connects to the test.sqlite db
c = conn.cursor()
#variable define section
tablename1 = 'users'
newfield = 'username'
newfield1 = 'password'
newfield2 = 'assigned_port'
newfield3 = 'lat'
newfield4 = 'long'
newfield5 = 'steps'
newfield6 = 'dummylogin'
newfield7 = 'dummyint'
newfield8 = 'dummypass'
newfield9 = 'fname'
fieldtype1 = 'INTEGER'
fieldtype2 = 'STRING'
fieldtype3 = 'FLOAT'
#creating a new sqlite column for each required field
c.execute('alter table {tn} add columm {cn} {ct}'\
.format(tn=tablename1, cn = newfield9, ct=fieldtype2))
c.execute('alter table {tn} add column {cn} {ct}'\
.format(tn=tablename1, cn=newfield, ct=fieldtype2))
c.execute('alter table {tn} add column {cn} {ct}'\
.format(tn=tablename1, cn=newfield1, ct=fieldtype2))
c.execute('alter table {tn} add column {cn} {ct}'\
.format(tn=tablename1, cn=newfield2, ct=fieldtype1))
c.execute('alter table {tn} add column {cn} {ct}'\
.format(tn=tablename1, cn=newfield3, ct=fieldtype3))
c.execute('alter table {tn} add column {cn} {ct}'\
.format(tn=tablename1, cn=newfield4, ct=fieldtype3))
c.execute('alter table {tn} add column {cn} {ct}'\
.format(tn=tablename1, cn=newfield5, ct=fieldtype1))
c.execute('alter table {tn} add column {cn} {ct}'\
.format(tn=tablename1, cn=newfield6, ct=fieldtype2))
c.execute('alter table {tn} add column {cn} {ct}'\
.format(tn=tablename1, cn=newfield7, ct=fieldtype1))
c.execute('alter table {tn} add column {cn} {ct}'\
.format(tn=tablename1, cn=newfield8, ct=fieldtype2))
conn.commit() #commits (saves) changes to the db
conn.close() #closes the connection to the db