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

problem with string

hi :)
I need some help for this script
I have
--------------------
cursor = conn.cursor()
cursor.execute("select * from playlist limit 5")
result = cursor.fetchall()
# iterate through resultset
playlist_txt = ''
for record in result:
mp3id = record[0]
mp3_title = record[1]
mp3_artist = record[2]
playlist_txt += mp3id + mp3_title + mp3_artist
#print mp3id , " - ", mp3_title , ' - ', mp3_artist , "<br />"
cursor.close()
conn.close()
------------------
#and want to print this out of "for record in result:"
print playlist_txt

#but there is an error in
playlist_txt += mp3id + mp3_title + mp3_artist

10x in advance :)
Jul 21 '05 #1
3 991
Tzanko Tzanev wrote:
#but there is an error in
playlist_txt += mp3id + mp3_title + mp3_artist


It'd be a lot easier to help if you'd say what the error was.
--
Michael Hoffman
Jul 22 '05 #2
Tzanko Tzanev wrote:
hi :)
I need some help for this script
I have
--------------------
cursor = conn.cursor()
cursor.execute("select * from playlist limit 5")
result = cursor.fetchall()
# iterate through resultset
playlist_txt = ''
for record in result:
mp3id = record[0]
mp3_title = record[1]
mp3_artist = record[2]
playlist_txt += mp3id + mp3_title + mp3_artist
#print mp3id , " - ", mp3_title , ' - ', mp3_artist , "<br />"
cursor.close()
conn.close()
------------------
#and want to print this out of "for record in result:"
print playlist_txt

#but there is an error in
playlist_txt += mp3id + mp3_title + mp3_artist


Have you considered doing "print record" at the appropriate place?

Care to tell us what the error message is, or is it a state secret that
can be obtained only by hanging you by your extremities and inserting
laxatives into your ears with firehoses?

Jul 22 '05 #3
Tzanko Tzanev a écrit :
hi :)
I need some help for this script
I have
please take care of indentation when posting code.
--------------------
cursor = conn.cursor()
cursor.execute("select * from playlist limit 5")
result = cursor.fetchall()
# iterate through resultset
playlist_txt = ''
for record in result: mp3id = record[0]
mp3_title = record[1]
mp3_artist = record[2]
playlist_txt += mp3id + mp3_title + mp3_artist

Err... what about:
print "<br />\n".join([" - ".join(record[:3]) for record in result])

or :
for record in result:
print "%s - %s - %s <br />" % tuple(record[:3])

Anyway, avoid string concatenation in loop, prefer appending to a list
and then str.join():

playlist = []
for record in result:
playlist.append(record[0] + record[1] + record[2])
print "\n".join(playlist)

Note that this is a very strange and complicated way to write:
print "\n".join([''.join(rec) for rec[:3] in result])

And, finally, if you just want to check what's in your resultset:
print result
#print mp3id , " - ", mp3_title , ' - ', mp3_artist , "<br />"
cursor.close()
conn.close()
Since you're doing a fetchall(), you could (and should if you don't use
em again) free your resources as soon as you've retrieved the resultset:

cursor = conn.cursor()
cursor.execute("select * from playlist limit 5")
result = cursor.fetchall()
cursor.close()
conn.close()
# now use the resultset...

------------------
#and want to print this out of "for record in result:"
print playlist_txt

#but there is an error in
playlist_txt += mp3id + mp3_title + mp3_artist


Are we supposed to guess what the error is ? Sorry, I'm not psychic
enough. Please post the full traceback.

(wild guess: mp3id is an integer ?)
Jul 22 '05 #4

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

Similar topics

18
by: muser | last post by:
is string converted to its integer equivalent by minusing it by 48? the function is suppose to check the fifth digit of struct member using the formula contained within the function. The function...
7
by: Forecast | last post by:
I run the following code in UNIX compiled by g++ 3.3.2 successfully. : // proj2.cc: returns a dynamic vector and prints out at main~~ : // : #include <iostream> : #include <vector> : : using...
6
by: lenny | last post by:
Hi, I've been trying to use a Sub or Function in VBA to connect to a database, make a query and return the recordset that results from the query. The connection to the database and the query...
18
by: Ian Stanley | last post by:
Hi, Continuing my strcat segmentation fault posting- I have a problem which occurs when appending two sting literals using strcat. I have tried to fix it by writing my own function that does the...
12
by: Jeff S | last post by:
In a VB.NET code behind module, I build a string for a link that points to a JavaScript function. The two lines of code below show what is relevant. PopupLink = "javascript:PopUpWindow(" &...
7
by: Ankit Aneja | last post by:
I put the code for url rewrite in my Application_BeginRequest on global.ascx some .aspx pages are in root ,some in folder named admin and some in folder named user aspx pages which are in user...
4
by: David Scemama | last post by:
Hi, I'm trying to read a database file written from a turbo Pascal program. I've set a structure to map the records in the file, but I have problem reading the file when I use VBFixedArray in...
16
by: Dany | last post by:
Our web service was working fine until we installed .net Framework 1.1 service pack 1. Uninstalling SP1 is not an option because our largest customer says service packs marked as "critical" by...
5
by: Stacey Levine | last post by:
I have a webservice that I wanted to return an ArrayList..Well the service compiles and runs when I have the output defined as ArrayList, but the WSDL defines the output as an Object so I was...
8
by: Rinaldo | last post by:
Hi, When I start my program in the debugger, there is no problem, but when not I get an exception. It appears in: private void Upload(string filename, string FTnaam) { MessageBox.Show("in...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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: 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
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...
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
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...
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...

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.