[Tutor] Data conversion - Problem solved

Joe Aquilina joe at chem.com.au
Thu May 19 10:07:27 CEST 2011


On 19/05/11 14:15, Joe Aquilina wrote:
> On 19/05/11 13:53, Andre Engels wrote:
>> On Thu, May 19, 2011 at 6:36 AM, Joe Aquilina<joe at chem.com.au>  wrote:
>>
>>> I am new to this list and very much a beginner to Python. Please 
>>> excuse me
>>> if this is a silly question, but in all my searches this morning I 
>>> have not
>>> been able to find an answer.
>>>
>>> I have a (single table) database file (SQLite3). It has one table, 
>>> call it
>>> literature, with an integer, autoincrement primary key field. I have 
>>> created
>>> a data entry form in Python that I want to use to enter new rows 
>>> into this
>>> database file. On the data entry form I will enter the values for a new
>>> table row - all except the primary key field.
>>>
>>> What I want to be able to do is to have my data entry form 
>>> autoincrement
>>> this primary key field for me, with no ability to change the 
>>> contents on the
>>> data entry form, and save this incremented value as the value of the 
>>> num
>>> field when I save the new row.
>>>
>>> So for example, if the last row in the table has a value of 256 in 
>>> the num
>>> field, I want the value of 257 to be saved as the value of the num 
>>> field
>>> into the new row I am adding, without having to see or or enter this 
>>> new
>>> value (or indeed the previous value) on the data entry screen.
>>>
>>> I hope this makes sense. But how do I do this?
>>>
>>> I thought about doing a
>>>
>>> SELECT num FROM literature;
>>>
>>> from the table, then getting the contents of the num field of the 
>>> last row
>>> in the data that a fetchall() retrieves and incrementing it to save 
>>> with the
>>> new row.
>>>
>>> However, the fetchall() returns the data as tuples, not integers and 
>>> I don't
>>> know how to convert from a tuple data type to an integer to make 
>>> this work.
>>>
>>> Is this possible? Or can I achieve my objective in some other way?
>>>
>>> Any advice/assistance would be appreciated. Thanks in advance.
>> l assume the tuple is a 1-tuple?
>>
>> Anyway, let fetch be the name of your tuple, then
>>
>> fetch[0]
>>
>> will be the first element of the tuple,
>>
>> fetch[1]
>>
>> the second element, and so on.
>>
>>
> Hello Andre.
>
> I realised after I read your response that I probably hadn't included 
> enough information, partly due to my  inexperience in Python and 
> partly due to haste on my part.
>
> AFter my original post, I had a little play in Python and was able to 
> create this tuple:
>
> [1, 2, 3, 4, 5]
>
> from which I was able to extract any item I wanted as an integer and 
> work with as I wanted. I am guessing that this is a 1-tuple.
>
> It is when I do the fetchall() from the table, that I get the following:
>
> [(1,), (2,), (3,)]
>
> I don't know enough to know whether this is a 1-tuple or not. It is 
> from this tuple that I want to extract the 3 as an integer so that I 
> can increment it and save as an integer into the next row in the table.
>
> Hope that is a little clearer.
>
> Cheers.
>
> Joe Aquilina

Please ignore this thread. I have solved my problem.

On further experimentation in Python, I found that if my SELECT 
statement included the whole row (or in fact at least two fields), the 
tuple returned was in a form that I could then extract the required 
value as an integer and then increment it as I wanted.

I have now incorporated this result into my code and it works as required.

Cheers.

Joe Aquilina


More information about the Tutor mailing list