473,406 Members | 2,549 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

DBApi Question with MySQL

Hi,

I'd like to modify some tables in a database in one transaction.
This approach doesn't work:

import MySQLdb

con = MySQLdb.connect("servernam", user = "username", passwd = "verysecret, db = "test", use_unicode = True, charset = "utf8")

cursor = con.cursor()

con.begin()

cursor.execute("delete from table")

do-some-stuff and wait

cursor.execute("insert into table value(%s, %s)", (1, 2))

con.commit()
When I look into the databse while the script is running, all rows from table are gone.
The expected behavior would be to see the new lines only when the script is finished.
The deletion should be (since inside a transaction) invisible up to the commit().

Has someone an idea how to use transactions correctly ?

What I need is this

start transaction

delete a lot of date in some tables (about 2 million rows)

insert a lot of new date in these tables (also about 2 million lines)

commit all changes, so all changes become visible here and only here.
Thanks a lot,

Greetings
Hans
Dec 12 '07 #1
1 1273
Hans Müller wrote:
Hi,

I'd like to modify some tables in a database in one transaction.
This approach doesn't work:

import MySQLdb

con = MySQLdb.connect("servernam", user = "username", passwd = "verysecret, db = "test", use_unicode = True, charset = "utf8")

cursor = con.cursor()

con.begin()

cursor.execute("delete from table")

do-some-stuff and wait

cursor.execute("insert into table value(%s, %s)", (1, 2))

con.commit()
When I look into the databse while the script is running, all rows from table are gone.
The expected behavior would be to see the new lines only when the script is finished.
The deletion should be (since inside a transaction) invisible up to the commit().

Has someone an idea how to use transactions correctly ?

What I need is this

start transaction

delete a lot of date in some tables (about 2 million rows)

insert a lot of new date in these tables (also about 2 million lines)

commit all changes, so all changes become visible here and only here.
Thanks a lot,

Greetings
Hans

Quick questions before going any further:

1) What's the table type in MySQL: ISAM, INNO, or ? As you probably know
MyISAM doesn't support transactions.

2) Is MySQL set to AutoCommit? Issue:
cursor.execute("select @@autocomit")
print cursor.fetchall()

If so try sending:
cursor.execute("set autocommit=0")

....and then doing the code you posted.


Paul

--
http://paulmcnett.com
Dec 12 '07 #2

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

Similar topics

9
by: Penn Markham | last post by:
Hello all, I am writing a script where I need to use the system() function to call htpasswd. I can do this just fine on the command line...works great (see attached file, test.php). When my...
2
by: pancho | last post by:
Greetings, I need help configuring/building PHP3 with MySQL as a DSO on a Solaris 8 box - this module is needed to host some existing sites I will be migrating Note. I built PHP4 from source and...
2
by: Rene Pijlman | last post by:
I can't seem to find any way to specify the character encoding with the DB API implementation of PyPgSQL. There is no mention of encoding and Unicode in the DB API v2.0 spec and the PyPgSQL README....
6
by: Martin Bless | last post by:
The good news: Along with Python-2.4 comes really good news to Windows users. Yes, you now CAN build extension modules yourself using the SAME C++ compiler and linker Python is built with...
6
by: Bob Parnes | last post by:
The following script is a one person's comparison of three methods for accessing a postgresql database using psycopg on a debian computer running python2.3. Following it are the results of running...
2
by: francescomoi | last post by:
Hi. I'm trying to build 'MySQL-python-1.2.0' on my Linux FC2: ---------------------------------- # export PATH=$PATH:/usr/local/mysql/bin/ # export mysqlclient=mysqlclient_r # python setup.py...
0
by: Greg Copeland | last post by:
According to the SQLAlchemy list, the DBAPI specification does not define a standard error reporting mechanism which would allow for generic detection of loss of database connection without DB...
1
by: chanshaw | last post by:
Alright so I got php running and installed i have mysql running and installed the thing im having a hard time with is having the php to call information from the mysql database. Im on Windows Vista...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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,...
0
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...
0
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...
0
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,...

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.