Advertisement
Guest User

Untitled

a guest
Nov 27th, 2014
168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.52 KB | None | 0 0
  1. Accessing Databases
  2.  
  3. We will look into Python's built-in SQLite DB module - sqlite3
  4.  
  5. I use Mozilla's plug-in SQLite manager tool to view the files I created with python, If you want to download Mozilla:
  6. https://www.mozilla.org/en-US/firefox/new/#download-fx
  7.  
  8.  
  9. SQLite Manager:
  10. https://addons.mozilla.org/en-US/firefox/addon/sqlite-manager/
  11.  
  12.  
  13.  
  14. The module to work with SQLLite in python: sqlite3
  15.  
  16. Following code will create and connect to a database, then execute a query against that database
  17.  
  18.  
  19.  
  20. import sqlite3
  21.  
  22. conn = sqlite3.connect("zoo.sqlite") #create connection to zoo.sqlite database, creates the database if it doesn't already exist
  23.  
  24. cursor = conn.cursor() #provides are cursor to the above connection (the means of executing the SQL queries)
  25.  
  26. cursor.execute("create table animal_count (name text, count integer)") #execute the create table query
  27.  
  28. cursor.execute("insert into animal_count(name, count) values('Elephant', 3)") #inset a row into the animal_count table
  29.  
  30. cursor.execute("insert into animal_count(name, count) values('Crocodile', 5)")
  31.  
  32. conn.commit() #commit changes to the database
  33.  
  34. conn.close() #close the connection
  35.  
  36.  
  37. zoo.sqlite will be created in the current working directory a better approach to pass data to a query is a parameterized query:
  38.  
  39. cursor.execute("insert into animal_count(name, count) values(?, ?)", ('Tiger', 2))
  40.  
  41. this approach escapes any special characters and also prevents SQLInjection.
  42.  
  43.  
  44. To fetch the records from database, assign the result of the SELECT execute statement to a variable, which will receive a list of tuples, each tuple representing a row of data
  45.  
  46.  
  47.  
  48.  
  49. import sqlite3
  50.  
  51. conn = sqlite3.connect("zoo.sqlite")
  52.  
  53. cursor = conn.cursor()
  54.  
  55. result = cursor.execute("select * from animal_count")
  56.  
  57. print(result.fetchall())
  58.  
  59. conn.commit()
  60.  
  61. conn.close()
  62.  
  63.  
  64. To loop through the data, use:
  65.  
  66.  
  67.  
  68. mport sqlite3
  69.  
  70. conn = sqlite3.connect("zoo.sqlite")
  71.  
  72. cursor = conn.cursor()
  73.  
  74. result = cursor.execute("select * from animal_count")
  75.  
  76. for row in result:
  77. print(row)
  78.  
  79. conn.commit()
  80.  
  81. conn.close()
  82.  
  83.  
  84. Executemany:
  85.  
  86. As an alternative to execute, you can use executemany, which takes a list of rows as parameter:
  87.  
  88.  
  89.  
  90. import sqlite3
  91.  
  92. conn = sqlite3.connect("zoo.sqlite")
  93.  
  94. cursor = conn.cursor()
  95.  
  96. animals=[('Frog', 10), ('Snake', 5), ('Turtle', 11)]
  97.  
  98. cursor.executemany("insert into animal_count(name, count) values(?, ?)", animals)
  99.  
  100. result = cursor.execute("select * from animal_count")
  101.  
  102. for row in result:
  103. print(row)
  104.  
  105. conn.commit()
  106.  
  107. conn.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement