[Tutor] make a sqlite3 database from an ordinary text file

avi.e.gross at gmail.com avi.e.gross at gmail.com
Mon Jun 20 12:46:55 EDT 2022


Manprit,

When something is not working, think about it before asking others to.

The variable called "heads" is what? Try printing it. 

It came from " yield line.split()" and it probably is a list containing two
items of text as in ["title1", "title2"]

So verify what is in it using print() or other tools before continuing.

Then look to see what your cur.execute() wants there. Does it want one
argument that is a list or does it want two individual arguments?

OR, does it need any arguments there if you DO IT YOURSELF!

What if you made a line for yourself I which you interpolated heads[1] and
heads[2] into a string and executed that?

A little search shows examples like:

cur.execute("select md5(?)", (b"foo",))

cur.execute("SELECT * FROM stocks WHERE symbol = '%s'" % symbol)


Forget the Select part and focus on how it puts something dynamically into
the string.

See if you can make your code in a way that passes the right thing. The
second example above is one of many ways of interpolating by replacing the
%s within quotes v=by the value of "symbol" and you have two things to
replace.

Time to go live my real life while you do what seems like homework.




-----Original Message-----
From: Tutor <tutor-bounces+avi.e.gross=gmail.com at python.org> On Behalf Of
Manprit Singh
Sent: Monday, June 20, 2022 10:35 AM
To: tutor at python.org
Subject: Re: [Tutor] make a sqlite3 database from an ordinary text file

 Dear Sir ,



 cur.execute("create table DATA(? INT, ? INT)",heads)

As in earlier mail I pointed out that the above command  will not work
because column names are not written inside the  single quotes in the
command create table.  Hence we can not use (?} place holders  .

Regards
Manprit Singh

On Mon, Jun 20, 2022 at 7:47 PM Manprit Singh <manpritsinghece at gmail.com>
wrote:

> Dear Sir,
>
> Many thanks for this . This mail has a lot of things for me . I Will 
> do all the exercises on my own and will revert you soon.
>
> Regards
> Manprit Singh
>
> On Mon, Jun 20, 2022 at 4:39 PM Alan Gauld via Tutor 
> <tutor at python.org>
> wrote:
>
>> On 20/06/2022 01:59, Manprit Singh wrote:
>>
>> > clear that the database table will have 2 columns, Column names of 
>> > the
>> > sqlite3 table are to be picked from the  first line of the text 
>> > file abcd.txt. (First column name will be Time, second column name 
>> > will be Pieces ).
>> > See i know this can be done very easily using numpy and Pandas. But 
>> > I am trying to do it with Core Python only.
>> >
>> > cur.execute("create table workdata(? INT, ? INT)", ("Time", 
>> > "Pieces"))
>> >
>> > I just want to insert column names (where these column names are  
>> > read from text file abcd.txt 1st line)  using a question mark style 
>> > placeholder.
>>
>> OK, Having shown how I would really do it, I'll now try to answer the 
>> question for pure Python:
>>
>> def get_data(open_file):
>>     for line in open_file:
>>         yield line.split()
>>
>>
>> # open the database/cursor here....
>>
>> with open('data.txt') as inf:
>>     heads = inf.readline().split()
>>     cur.execute("create table DATA(? INT, ? INT)",heads)
>>     cur.executemany("insert into workdata values (?, 
>> ?)",get_data(inf))
>>
>> cur.execute("select * from DATA")
>> for row in cur.fetchall():
>>     print(row)
>>
>>
>> Note, I didn't actually run this but I think it should be close.
>>
>> --
>> Alan G
>> Author of the Learn to Program web site http://www.alan-g.me.uk/ 
>> http://www.amazon.com/author/alan_gauld
>> Follow my photo-blog on Flickr at:
>> http://www.flickr.com/photos/alangauldphotos
>>
>>
>> _______________________________________________
>> Tutor maillist  -  Tutor at python.org
>> To unsubscribe or change subscription options:
>> https://mail.python.org/mailman/listinfo/tutor
>>
>
_______________________________________________
Tutor maillist  -  Tutor at python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor



More information about the Tutor mailing list