473,789 Members | 2,419 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Simple sqlite3 question

cjl
P:

I am using python 2.5.1 on windows. I have the following code:

conn = sqlite3.connect ('.\optiondata' )
c = conn.cursor()
try:
c.execute('''cr eate table options (ssymbol text, strike real,
osymbol text, bid real, mpp real, upp real)''')
except sqlite3.Operati onalError:
pass

I am hoping the above code creates a new database file named
'optiondata' with a single table named 'options', or connects to it if
already created. Am I off the mark here?

I also have a function that does the following:

c.execute("""in sert into options values (?,?,?,?,?,?)"" ",data)

When I run the script and there is no file named optiondata, one is
created and the correct data is added to it. If I run the script
again then the data from the first run seems to be replaced with the
data from the second run. I expected that the data from the second run
would be appended to the database file, not replace it.

Can anyone point me in the right direction to get the expected
behavior?

-cjl

Apr 24 '07 #1
3 1908
On 24 Apr 2007 10:03:45 -0700, cjl <cj****@gmail.c omwrote:
When I run the script and there is no file named optiondata, one is
created and the correct data is added to it. If I run the script
again then the data from the first run seems to be replaced with the
data from the second run. I expected that the data from the second run
would be appended to the database file, not replace it.
It sounds like you're not calling conn.commit() before you close the
database. By default sqlite (and any other DBAPI 2.0 compliant sql
wrapper) will start in transactional mode. You need to commit() your
transactions or they will be rolled back when you close the database
connection. If you don't want to call commit on your own, you can
switch the database into autocommit mode by setting the isolation
level to None when you open the connection, like this:

conn = sqlite3.connect ('.\optiondata' , isolation_level =None)

--
Jerry
Apr 24 '07 #2
Jerry Hill wrote:
On 24 Apr 2007 10:03:45 -0700, cjl <cj****@gmail.c omwrote:
>When I run the script and there is no file named optiondata, one is
created and the correct data is added to it. If I run the script
again then the data from the first run seems to be replaced with the
data from the second run. I expected that the data from the second run
would be appended to the database file, not replace it.


It sounds like you're not calling conn.commit() before you close the
database. By default sqlite (and any other DBAPI 2.0 compliant sql
wrapper) will start in transactional mode. You need to commit() your
transactions or they will be rolled back when you close the database
connection. If you don't want to call commit on your own, you can
switch the database into autocommit mode by setting the isolation
level to None when you open the connection, like this:

conn = sqlite3.connect ('.\optiondata' , isolation_level =None)
Don't do that; make the commit calls. Otherwise you'll be back
here complaining about how bad stuff happened when two copies of
the program ran at the same time.

John Nagle
Apr 24 '07 #3
cjl <cj****@gmail.c omwrote:
>
I am using python 2.5.1 on windows. I have the following code:

conn = sqlite3.connect ('.\optiondata' )
This is unrelated to your question, but you have a slash problem there. \o
doesn't happen to be a valid escape character, but if you had used
"testdata" as the filename, it would have failed.

You should use one of these alternatives:

conn = sqlite3.connect ('.\\optiondata ')
conn = sqlite3.connect (r'.\optiondata ')
conn = sqlite3.connect ('./optiondata')
conn = sqlite3.connect ('optiondata')
--
Tim Roberts, ti**@probo.com
Providenza & Boekelheide, Inc.
Apr 26 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
2196
by: lolmcbride | last post by:
Hi, is it possible to pass args through the api which are the same as the args you can use on the sqlite3 command line? What I'm talking about is the .mode or .output commands which you can enter via the sqlite3 cli so I can dynamically change were the output goes. Ta
2
1974
by: John Machin | last post by:
Apologies in advance if this is a bit bloggy, but I'd like to get comments on whether I've lost the plot (or, more likely, failed to acquire it) before I start reporting bugs etc. From "What's new ...": """ # Create table c.execute('''create table stocks (date timestamp, trans varchar, symbol varchar, qty decimal, price decimal)''')
8
3559
by: rdrink | last post by:
I am just getting into pysqlite (with a fair amount of Python and MySQL experience behind me) and have coded a simple test case to try to get the hang of things... yet have run into a 'stock simple' problem... I can create a database 'test.db', add a table 'foo' (which BTW I repeatedly DROP on each run) with one INTGER column 'id', and can insert data into with: cur.execute("INSERT INTO foo (id) VALUES (200)") con.commit()
6
3234
by: Jorgen Bodde | last post by:
Hi all, I am using sqlite3 in python, and I wonder if there is a way to know if there are valid rows returned or not. For example I have a table song with one entry in it. The ID of that entry is 1, so when I do; .... print s .... (1, u'Spikedrivers Blues', u'Mississippi John Hurt')
4
8925
by: Simon | last post by:
I installed the source code on unix for python 2.5.1. The install went mainly okay, except for some failures regarding: _ssl, _hashlib, _curses, _curses_panel. No errors regarding sqlite3. However, when I start python and do an import sqlite3 I get: /ptmp/bin/python Python 2.5.1 (r251:54863, May 29 2007, 05:19:30) on sunos5
33
2567
by: Stef Mientki | last post by:
hello, I discovered that boolean evaluation in Python is done "fast" (as soon as the condition is ok, the rest of the expression is ignored). Is this standard behavior or is there a compiler switch to turn it on/off ? thanks, Stef Mientki
3
2974
by: milan_sanremo | last post by:
I have sqlite installed, but when I try to import sqlite3 I receive: Python 2.5.1 (r251:54863, Nov 3 2007, 02:54:36) on sunos5 Type "help", "copyright", "credits" or "license" for more information. Traceback (most recent call last): File "<stdin>", line 1, in <module> ImportError: No module named sqlite3 Yet:
0
1763
by: Ben Lee | last post by:
hi folks -- a quick python and sqlite3 performance question. i find that inserting a million rows of in-memory data into an in-memory database via a single executemany() is about 30% slower than using the sqlite3 CLI and the .import command (reading the same data from a disk file, even.) i find this surprising, executemany() i assume is using a prepared statement and this is exactly what the .import command does (based on my quick...
15
14790
by: Kurda Yon | last post by:
Hi, I try to "build" and "install" pysqlite? After I type "python setup.py build" I get a lot of error messages? The first error is "src/ connection.h:33:21: error: sqlite3.h: No such file or directory". So, I assume that the absence of the "sqlite3.h" is the origin of the problem. I found on the web, that this file should be either in "/usr/local/ include" or in "/usr/local/lib". I check this directories and I really
0
9663
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10404
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10136
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9979
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...
1
7525
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
6761
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();...
1
4089
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
2
3695
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2906
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.