Connect with Expertise | Find Experts, Get Answers, Share Insights

Python function for reading and manipulate sqlite table

 
Join Date: Jan 2010
Posts: 6
#1: Jan 27 '10
I am new in python and pysqlite. Right now, I am reallly trying to figure it out the way that I can make python function read the table in sqlite database and calculate data using SQL language.

For example:
I do have database name "CART.sqlite" with table name "oritab"

now I do write the function in python as follow (real code is indented)
************************************************** **********************
Expand|Select|Wrap|Line Numbers
  1. from pysqlite2 import dbapi2 as sqlite
  2. conn = sqlite.connect('C:\CART.sqlite')
  3. ex = cur.execute
  4.  
  5. def deviance(set):
  6.     SQL_AvgYj = "SELECT AVG(DurationTotal) As Results FROM set"
  7.     SQL_N = "SELECT COUNT(DISTINCT P_CODE) AS N FROM set"
  8.     SQL_Nmin = "SELECT MIN(P_CODE) AS Nmin FROM set"
  9.     SQL_Nmax = "SELECT MAX(P_CODE) AS Nmax FROM set"
  10.     AvgYj = ex(SQL_AvgYj).fetchall()
  11.     N = ex(SQL_N).fetchall()
  12.     Nmin = ex(SQL_Nmin).fetchall()
  13.     Nmax = ex(SQL_Nmax).fetchall()
  14.     count = Nmin
  15.     S = 0
  16.     while count <= Nmax:
  17.         actcount = 1
  18.         while actcount <= 30:
  19.             Yij = ex("SELECT SUM(DurationTotal) FROM set WHERE   P_CODE=count AND ActCode=actcount")
  20.             S += (Yij-AvgYj)**2
  21.             actcount += 1
  22.         count += 1
  23.     return S
************************************************** ***************************
then I call function as follow

result = deviance(oritab)
************************************************** **************************
Seem like it wont work this way. python is not recognize the table name at all.
I really confuse how can I dump the table from sqlite into python function.
could any one can help me or guide me into the right direction pls
Attached Files
File Type: txt func.txt (797 Bytes, 21 views)
best answer - posted by bvdet
Please use code tags when posting code. See our posting guidelines.

An initial observation - table name "oritab" is passed to function deviance() and assigned to identifier "set". You can use string formatting to pass the string "oritab" to pysqlite. Example:

Expand|Select|Wrap|Line Numbers
  1. SQL_AvgYj = "SELECT AVG(DurationTotal) As Results FROM %s" % (set)
BV - Moderator

bvdet's Avatar
E
M
C
 
Join Date: Oct 2006
Location: Nashville, TN
Posts: 1,926
#2: Jan 27 '10

re: Python function for reading and manipulate sqlite table


Please use code tags when posting code. See our posting guidelines.

An initial observation - table name "oritab" is passed to function deviance() and assigned to identifier "set". You can use string formatting to pass the string "oritab" to pysqlite. Example:

Expand|Select|Wrap|Line Numbers
  1. SQL_AvgYj = "SELECT AVG(DurationTotal) As Results FROM %s" % (set)
BV - Moderator
 
Join Date: Jan 2010
Posts: 6
#3: Feb 8 '10

re: Python function for reading and manipulate sqlite table


Thank you for your help BV
Reply