473,386 Members | 1,842 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,386 software developers and data experts.

MySQL Select, from tuple to list

Hopefully I can explain this correctly. I am trying to take a MySQL select statement, get the output into a list, then upload that list back to the MySQL server.

Expand|Select|Wrap|Line Numbers
  1. cursor.execute("SELECT stuff FROM place WHERE thing = 3 ORDER BY track_id ASC")
This spits out a tuple of integers ...

((1L,), (2L,), (3L,), (4L,), (5L,), (6L,), (7L,), (8L,), (9L,), (10L,), (11L,))

... with an 'L' following each one.

But I want to update the database with this set as:

1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11

At first I thought I had it, but each converted row of tuples was the same thing even though the actually data pulled had changed. Anyone have any ideas how to do this?
Dec 9 '11 #1

✓ answered by sithlordkyle

Actually, I figured it out myself...

Expand|Select|Wrap|Line Numbers
  1. for n in range(0,75):
  2.   cursor.execute("SELECT row FROM table WHERE column = %s ORDER BY column ASC", (n))
  3.   result = cursor.fetchall()
  4.   List = list()
  5.   for t in result:
  6.     List.append(int(t[0]))
  7.   List = ",".join(map(str,List))
  8.   ListUpdate = cursor.execute("UPDATE table SET column = %s WHERE column = %s", (str(List),n))
  9.  
This will take the SELECT statement from MySQL as a tuple, format it into a list without the L or parenthesis, then UPDATE the MySQL with the list.

3 18251
Glenton
391 Expert 256MB
It sounds like you want to convert something from a tuple containing a long integer into an integer?

Expand|Select|Wrap|Line Numbers
  1. for t in ((1L,), (2L,), (3L,), (4L,), (5L,), (6L,), (7L,), (8L,), (9L,), (10L,), (11L,)):
  2.     print int(t[0])
Dec 9 '11 #2
Glenton, thanks for the response. I actually got it to give me the list without the L in it. But that's half the thing. I need to dump those numbers back into a list and insert that list into a MySQL database.

I think that PHP will provide a better solution than Python at this point.
Dec 12 '11 #3
Actually, I figured it out myself...

Expand|Select|Wrap|Line Numbers
  1. for n in range(0,75):
  2.   cursor.execute("SELECT row FROM table WHERE column = %s ORDER BY column ASC", (n))
  3.   result = cursor.fetchall()
  4.   List = list()
  5.   for t in result:
  6.     List.append(int(t[0]))
  7.   List = ",".join(map(str,List))
  8.   ListUpdate = cursor.execute("UPDATE table SET column = %s WHERE column = %s", (str(List),n))
  9.  
This will take the SELECT statement from MySQL as a tuple, format it into a list without the L or parenthesis, then UPDATE the MySQL with the list.
Dec 15 '11 #4

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

Similar topics

3
by: hokieghal99 | last post by:
Hi, I'd like to get user input from an html form into a mysql select statement. Here's where I'm stumped: $result = mysql_query("SELECT * FROM dept WHERE notes LIKE '%search-string%'",$db); ...
23
by: phpfrizzle | last post by:
Hi there, I have a site with products on it. The site has a mysql backend. All products belong to certain series (table series). There can be up to 4 different products (table products)...
7
by: mike-nospam | last post by:
Trying to write a php script where the user will enter the row number (index) of a record. Say, for example, user wants record 55: : select * from MyTable where Index = "55" where Index is an...
9
by: jossinet | last post by:
Bonjour, j'ai une table : matable dans laquelle j'ai un champs : codepostal Ce champs code postal est rempli d'enregistrements qui ont des valeurs de 5 chiffres. Je souhaite créer un bouton...
4
by: Eric Freeman | last post by:
Is there any way in Postgres to SELECT a list of table names from inside of a C program using ECPG? Something similar to SELECT current_user that will give you all of the tables in the database...
4
by: VK | last post by:
I'm looking for autoexpand <select> list onfocus and collapse it back onselect/onblur (the list is select-one type) I know it is not possible directly, but I've seen here a hack by changing...
2
by: vncntj | last post by:
I'm trying to get the venue in the 'List' column when in contains a value. But if the venue Is Null then place the event in the 'List' SELECT CASE WHEN event Is Null THEN venue Else event...
3
by: Beholder | last post by:
I hope that someone can help me with the following: Short background explenation: I have a shrfepoint page (newform.aspx) in a item list. On this page is a lookup column that displays a lookup...
5
by: =?Utf-8?B?bWNhdWxpZmZl?= | last post by:
I have an old application ( pre-VB5) that I need to add a select/option list to. This is an edit program so the values for the form will be retrieved from a database. How do I set the value of...
0
by: Andrus | last post by:
How to create select columns list dynamically in DLinq ? I want to create something like Console.WriteLine("Enter list of columns to return, separated by commas:"); string list =...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.