Help me with this code PLEASE
Denis McMahon
denismfmcmahon at gmail.com
Tue Nov 5 15:10:09 EST 2013
On Tue, 05 Nov 2013 20:09:42 +0200, Nick the Gr33k wrote:
> Denis, i have already provided my code trying to do what i need and i
> need some commendation on how to make it work.
Nick, you're obviously trying to code way above your abilities.
If you want me to write your code, you will have to pay my day rate, and
you can't afford it. If you could afford it, you'd be paying someone
competent already.
Here, have a code example, this works, although the formatting might get
broken. The database "ntg1" has a single table called str_idx defined as
follows:
create table idx_str ( idx int primary key, str varchar[1024] );
#!/usr/bin/python
import random
import sqlite3
def make_list( length ):
l = []
if ( length < 1 ):
return l
for count in range( 0, length ):
s = '';
for i in range( 1, random.randrange( 4, 12 ) ):
c = chr( random.randrange( 97, 123 ) )
s += c
l.append( s )
return l
def list_to_string( l ):
return "|".join( l )
def string_to_list( s ):
return s.split( "|" )
l1 = make_list( 10 )
print "l1 -> ", l1
s = list_to_string( l1 )
print "s -> ", s
l2 = string_to_list( s )
print "l2 -> ", l2
print "l1 == l2 -> ", l2 == l1
l2 = make_list( 10 )
l3 = make_list( 10 )
l4 = make_list( 10 )
print "Original Lists"
print "l1 -> ", l1
print "l2 -> ", l2
print "l3 -> ", l3
print "l4 -> ", l4
conn = sqlite3.connect( "ntg1" )
cur = conn.cursor()
cur.execute( "delete from idx_str where idx is not null" )
cur.execute(
"insert into idx_str values ( 1, '{0}' )".format(
list_to_string( l1 ) ) )
cur.execute(
"insert into idx_str values ( 2, '{0}' )".format(
list_to_string( l2 ) ) )
cur.execute(
"insert into idx_str values ( 3, '{0}' )".format(
list_to_string( l3 ) ) )
cur.execute(
"insert into idx_str values ( 4, '{0}' )".format(
list_to_string( l4 ) ) )
conn.commit()
conn.close()
print "Lists now in DB"
print "Reading 1 record at a time"
conn2 = sqlite3.connect( "ntg1" )
cur2 = conn2.cursor()
cur2.execute( "select * from idx_str where idx = 1" );
row = cur2.fetchone()
print "stored 1 -> ", row[ 1 ]
print "l1 -> ", string_to_list( row[ 1 ] )
cur2.execute( "select * from idx_str where idx = 2" );
row = cur2.fetchone()
print "stored 2 -> ", row[ 1 ]
print "l2 -> ", string_to_list( row[ 1 ] )
cur2.execute( "select * from idx_str where idx = 3" );
row = cur2.fetchone()
print "stored 3 -> ", row[ 1 ]
print "l3 -> ", string_to_list( row[ 1 ] )
cur2.execute( "select * from idx_str where idx = 4" );
row = cur2.fetchone()
print "stored 4 -> ", row[ 1 ]
print "l4 -> ", string_to_list( row[ 1 ] )
conn2.close()
print "Reading all records at once"
conn3 = sqlite3.connect( "ntg1" )
cur3 = conn3.cursor()
cur3.execute( "select * from idx_str" );
row = cur3.fetchone()
while not row == None:
print "stored ", row[ 0 ], " -> ", row[ 1 ]
print "list ", row[ 0 ], " -> ", string_to_list( row[ 1 ] )
row = cur3.fetchone()
conn3.close()
One thing you haven't considered, what happens if a user has so many
downloads in his list that the converted list doesn't fit in the declared
string column width in your database?
--
Denis McMahon, denismfmcmahon at gmail.com
More information about the Python-list
mailing list