473,398 Members | 2,335 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,398 software developers and data experts.

MySQL help

So i'm writing this program to check if a row exists in a table. If it
doesn't it inserts it if it does it will update that row with the
current info.

Well it sorta works but not fully. It goes through and executes the
correct querries but when it comes to determining if the row exists it
doesn't get back a result. Yet if I mannually enter it into the mysql
console I get a result. This only happens when my if statement to
determine if there was a result returned is not commented out. If it
is commented out it returns that the row exists. Any help would be
great.

Thanks.

here is a snippet of the code assume a cursor has been defined, import
MySQL has occured and the connection have all taken place.

"a" is the table the query is taking place on.
name is nodeXXX and b is either a 0-3 number.
check="SELECT * FROM "+a+" WHERE nodeid='"+name+"' AND
lid='"+b+"'"
result = cursor.execute(check)
numrows = int(cursor.rowcount)
print "numrows:", numrows
cursor = db.cursor()
if numrows == 0:
output="INSERT INTO "+a+" SET nodeid='"+name+"',
lid='"+b+space+", ".join(v)
cursor.execute(output)
db.commit()
print "Insert"
result=''
else:
output="UPDATE "+a+" SET nodeid='"+name+"',
lid='"+b+space+", ".join(v)
cursor.execute(output)
result=''
db.commit()
print "Updating"

id.append(int(db.insert_id()))
Thanks,

Aug 1 '05 #1
5 1218
su***********@gmail.com wrote:
So i'm writing this program to check if a row exists in a table. If it
doesn't it inserts it if it does it will update that row with the
current info.


(...)

quick tip: are you aware of the mysql's REPLACE command?
<http://dev.mysql.com/doc/mysql/en/replace.html>
--
deelan, #1 fan of adriana lima!
<http://www.deelan.com/>
Aug 1 '05 #2
I wasn't aware of the replace command I'll take a look at that tomorrow
and see if it helps. Thanks

quick tip: are you aware of the mysql's REPLACE command?
<http://dev.mysql.com/doc/mysql /en/replace.html>


Aug 1 '05 #3
On 1 Aug 2005 11:58:37 -0700, "su***********@gmail.com"
<su***********@gmail.com> declaimed the following in comp.lang.python:

here is a snippet of the code assume a cursor has been defined, import
MySQL has occured and the connection have all taken place.
Hopefully in the order of import, connection, cursor <G>
"a" is the table the query is taking place on.
name is nodeXXX and b is either a 0-3 number.
check="SELECT * FROM "+a+" WHERE nodeid='"+name+"' AND
lid='"+b+"'"
"b" had better be a STRING or this is nonsense...

Better would be:

check = 'select * from %s where nodeid="%s" and lid="%s"' % (a,
name, b)
result = cursor.execute(check)
I don't have experience to know if select and update can use the
notation used for insert... If it does, the above would be

check = 'select * from %s where nodeid=%%s and lid=%%s" % (a,)
result = cursor.execute(check, (name, b))
numrows = int(cursor.rowcount)
print "numrows:", numrows
cursor = db.cursor()
if numrows == 0:
output="INSERT INTO "+a+" SET nodeid='"+name+"',
lid='"+b+space+", ".join(v)
Pardon? Does SQL insert accept "set" notation?

output = 'insert into %s (nodeid, lid) values (%s, %s)'
cursor.execute(output)
cursor.execute(output, (name, b))
db.commit()
print "Insert"
result=''
else:
output="UPDATE "+a+" SET nodeid='"+name+"',
lid='"+b+space+", ".join(v)
That's going to set EVERY record's nodeid and lid to the same
values. Normally update is used with a where clause to identify which
records are to be changed. Of course, you also have this strange case
where you are trying to set the fields to the SAME values you used in
the select -- there is no apparent "update" here if you are trying to
ONLY affect the record found by select; IE

output = 'update %s set nodeid=%%s,lid=%%s where nodeid=%%s and
lid=%%s' % (a,)
cursor.execute(output, (name, b, name, b))

-- ================================================== ============ <
wl*****@ix.netcom.com | Wulfraed Dennis Lee Bieber KD6MOG <
wu******@dm.net | Bestiaria Support Staff <
================================================== ============ <
Home Page: <http://www.dm.net/~wulfraed/> <
Overflow Page: <http://wlfraed.home.netcom.com/> <

Aug 2 '05 #4
Hey Dennis thanks for the tips I haven't had a chance to take another
stab at that code yet but I think I may try some of your suggestions.
The SQL statements are valid but something doesn't appear to work right
I may try and switch them to what you're suggesting and see if that
helps with my problem.

Thanks,

Aug 3 '05 #5
On 3 Aug 2005 08:37:38 -0700, "su***********@gmail.com"
<su***********@gmail.com> declaimed the following in comp.lang.python:
Hey Dennis thanks for the tips I haven't had a chance to take another
stab at that code yet but I think I may try some of your suggestions.
The SQL statements are valid but something doesn't appear to work right
I may try and switch them to what you're suggesting and see if that
helps with my problem.
The DB-API spec is that the
cursor.execute(parameterized_statement, (parameters,))
will properly quote arguments -- strings get quote marks, numerics are
left alone, etc.

-- ================================================== ============ <
wl*****@ix.netcom.com | Wulfraed Dennis Lee Bieber KD6MOG <
wu******@dm.net | Bestiaria Support Staff <
================================================== ============ <
Home Page: <http://www.dm.net/~wulfraed/> <
Overflow Page: <http://wlfraed.home.netcom.com/> <

Aug 4 '05 #6

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

Similar topics

0
by: Ryan Schefke | last post by:
------=_NextPart_000_0077_01C34C8B.2B90C960 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit ..I just sent this out to the win32 distribution list but no one has...
0
by: Girish Agarwal | last post by:
--0-474210375-1058976151=:31789 Content-Type: text/plain; charset=us-ascii Content-Id: Content-Disposition: inline Note: forwarded message attached. __________________________________
0
by: Yun Guan | last post by:
Hello mysql gurus, I am trying to run perl on mysql database on Red Hat box. I want to install DBI and DBD:mysql using CPAN: perl -MCPAN -e shell cpan>install DBI The above succeeded, but...
0
by: Mark Adams | last post by:
I am a relative newbie to MySQL. I had a Postfix+Courier+MySQL mail server running for several months. It took me a week or so to get it up and running in September. Now, I did a clean upgrade to...
3
by: Kirk Soodhalter | last post by:
Hi, This started as a phpmyadmin problem, but has somehow morphed into a mysql problem. I don't know how to fix it. I am posting the conversation from a php newsgroup since it started there. ...
2
by: Saqib Ali | last post by:
I installed mySQL and have it running.... but I think I made a mistake somewhere along the line...... I believe I did follow the instructions that were provided with the distribution at:...
1
by: Alex Hunsley | last post by:
I am trying to install the DBD::mysql perl module. However, it claims I need mysql.h: cpan> install DBD::mysql CPAN: Storable loaded ok Going to read /home/alex/.cpan/Metadata Database was...
5
by: MLH | last post by:
I'm supposed to set a password for the MySQL root user. The output of mysql_install_db instructed me to run the following commands... /usr/bin/mysqladmin -u root -h appserver password mynwewpasswd...
15
by: Cheryl Langdon | last post by:
Hello everyone, This is my first attempt at getting help in this manner. Please forgive me if this is an inappropriate request. I suddenly find myself in urgent need of instruction on how to...
1
by: PowerLifter1450 | last post by:
I've been having a very rough time installinig mySQL on Linux. I have been following the instructions form here: http://www.hostlibrary.com/installing_apache_mysql_php_on_linux Everytime I get to...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
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.