×

Discussion Board

Results 1 to 5 of 5
  1. #1
    Registered User
    Join Date
    Feb 2012
    Posts
    18

    Database Question

    Hi all,
    Hope this the right place to post a new question? I had put it in a existing thread but thought a new one maybe more likley to be viewed
    I'm trying to write to a database using the method detailed in the joe bloggs example, it works fine, I have a database full of joe blogs!
    However I wish to be able to enter a variable got from the user, I'm new to this so forgive me if this is a obvious question, but how do you do it?

    db.execute(u"insert into table values(1,'joe','bloggs')")

    this is the code that works, I've put variable names in different places with no joy, here is my last attemt:

    name=(u"Name?")
    job=(u"Job?")
    hobby=(u"Hobby")
    sql= "insert into table1 (forename,surname,pastime) values('%s','%s','%s')"%(name,job,hobby)
    unicode(sql)
    db.execute(sql)

    It doesnt return any errors like my previous attempts, but it doesnt write to the db either!

    Any help would be welcome,
    Thanks Geoff

  2. #2
    Nokia Developer Moderator
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    28,689

    Re: Database Question

    It might be a good idea to display the resulting SQL statement you are executing.
    Otherwise do you really have a 'pastime' field? What happens if you remove it for a try, and use
    Code:
    sql= "insert into table1 (forename,surname) values('%s','%s')"%(name,job)
    ?

  3. #3
    Registered User
    Join Date
    Feb 2012
    Posts
    18

    Re: Database Question

    Thanks for that, changed it to:
    db.execute(u"insert into table1 (forename,surname) values('%s','%s')"%(name,job))
    Which works ok

    I thought I had created a pastime field with the following code:

    db.execute(u"create table table1 (id count,forename VARCHAR,surname VARCHAR,pastime VARCHAR)")

    Is there a limit to how many fields I can have?

  4. #4
    Registered User
    Join Date
    Feb 2012
    Posts
    18

    Re: Database Question

    Ah I think I've sussed it! I was trying to create a table that had already been created! Hence why I didn't have a 'pastime' field, this code gets data from user puts it in a database then spews it out again:

    Code:
    import e32db, appuifw
    
    #initiate database objects
    db=e32db.Dbms()
    dbv=e32db.Db_view()
    db.close()
    
    
        
    #open database, if doesent exist, create file
    try:
        db.open(u"e:\\test2.db")
    except:
        db.create(u"e:\\test2.db")
        db.open(u"e:\\test2.db")
    #create table with four fields
    try:
       db.execute(u"create table table2 (forename VARCHAR,surname VARCHAR,pastime VARCHAR,interest VARCHAR)")
    except:
       print(u"")
    
    name=appuifw.query(u"Name?","text")
    job=appuifw.query(u"Job?","text")
    hobby=appuifw.query(u"Hobby","text")
    interest=appuifw.query(u"Interests","text")
    
    
    #to insert data
    
    
    db.execute(u"insert into table2 (forename,surname,pastime,interest) values('%s','%s','%s','%s')"%(name,job,hobby,interest))
    
    
    
    #fetch whole datasheet
    dbv.prepare(db,u"select * from table2")
    
    for i in range(1,dbv.count_line()+1):
        dbv.get_line() #gets data from selected row
        for i in range(1,dbv.col_count()+1):
            print dbv.col(i) #prints each column data
        dbv.next_line() #next row
    db.close()
    Thanks for looking at it, any hints or comments welcome.

  5. #5
    Nokia Developer Moderator
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    28,689

    Re: Database Question

    Yes, that was the idea I wanted you to check.
    Since modifying the data model usually happens in development time only, the usual approach is to simply delete the file manually (with the File Manager for example) prior to trying the new data model.

Similar Threads

  1. Database Question/s
    By Leusoj in forum Symbian
    Replies: 6
    Last Post: 2009-07-21, 10:00
  2. A question about the Calender Database
    By wssjtc in forum Symbian
    Replies: 0
    Last Post: 2007-01-27, 18:29
  3. DataBase Question
    By Spider_Eg in forum Mobile Java General
    Replies: 0
    Last Post: 2005-11-21, 16:31
  4. About the database question
    By mingkit in forum Symbian
    Replies: 0
    Last Post: 2003-09-19, 19:53

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
×