473,651 Members | 2,644 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Python function for reading and manipulate sqlite table

6 New Member
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.sqlit e" 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, 341 views)
Jan 27 '10 #1
2 2655
bvdet
2,851 Recognized Expert Moderator Specialist
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
Jan 27 '10 #2
natachai
6 New Member
Thank you for your help BV
Feb 8 '10 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

3
2414
by: Michael Goettsche | last post by:
Hello guys, I succeeded in convincing my CS teacher to use Python and Sqlite instead of Microsoft Access to get started with databases. We are working on a windows terminal server to which I have no admin access, so I'd like to ask you which module is best suited to use Sqlite with Python under windows. The best would be a module which is easy to install without further dependencies. Thanks in advance,
10
3681
by: Andrew Dalke | last post by:
Is there an author index for the new version of the Python cookbook? As a contributor I got my comp version delivered today and my ego wanted some gratification. I couldn't find my entries. Andrew dalke@dalkescientific.com
8
2294
by: Vittorio | last post by:
I am reading "Beginning Python from Novice to Professional" and the book is really awesome. Nonetheless on ch 13 "Database Support" I found this code to import data (in a txt file) into a SQLite Database: #this was corrected because original "import sqlite" does not work from pysqlite2 import dbapi2 as sqlite #this function strips the txt file from special chars def convert(value): if value.startswith('~'):
1
3206
by: jander22 | last post by:
Hi, I just started playing with Python and I am working on a program that pulls data from a table in Oracle and creates a similar table in SQLite. Basically, I want to be able to do this: create table tab1 as select * from tab2 where tab1 is an SQLite table and tab2 is an Oracle table. Is this possible? I have 2 connection objects, but when I try to run this code Python replies that tab2 cannot be found. I am thinking it is trying...
0
1591
by: | last post by:
Greetings. In an effort to get python2.4 on my Centos 3.7, I installed the python bootstrap rpm. This installed 2.4 alongside 2.2 and updated yum to 2.4.0. Oddly, it didn't create a symlink 'python' for either 2.2 or 2.4. I also get a series of troubling dependency errors when I run yum update. Below is the output of the bootstrap install, which includes both a failure early on (possibly related to alternatives and/or the symlink?)...
12
7887
by: BartlebyScrivener | last post by:
Sorry, I have a knack for making things more difficult than they need to be. I'm on Windows XP and waiting for the ActiveState release of Python 2.5. I want to experiment with sqlite and I see that Python 2.5 has the "wrapper" but sqlite itself must be downloaded separately. I see no installation instructions on the sqlite.org site for Windows XP. I have downloaded sqlite3.exe, sqlite3.dll, and sqlite3.def. Must I put them in a certain...
11
4344
by: walterbyrd | last post by:
With PHP, libraries, apps, etc. to do basic CRUD are everywhere. Ajax and non-Ajax solutions abound. With Python, finding such library, or apps. seems to be much more difficult to find. I thought django might be a good way, but I can not seem to get an answer on that board. I would like to put together a CRUD grid with editable/deletable/
9
2599
by: Ed Leafe | last post by:
On Apr 21, 2008, at 1:05 PM, Daniel Fetchinson wrote: Don't most binary distributions include SQLite itself? I installed 2.5.2 on a new WinXP VM, and SQLite is working fine. -- Ed Leafe
0
2568
by: Joe Goldthwaite | last post by:
Thanks Guilherme. That helped. I guess I was thinking that pysqlite would automatically come with some version of sqlite. The fact that it doesn't is what was causing me to get the strange results. I downloaded the Windows version of the SQLite3.dll. I didn't know where to put it so I first put it in its own directory and tried to register it. That didn't work so I just moved it to the pysqlite directory in site-packages. That did...
0
8275
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8576
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7296
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6157
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5609
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4143
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2696
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1906
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1585
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.