×

Discussion Board

Results 1 to 14 of 14
  1. #1
    Registered User
    Join Date
    May 2009
    Location
    italia
    Posts
    9

    Question errors in creating the database

    hello to all,

    I am having problems creating a table in the database, I can not understand how to resolve.
    attaching the code for you to see

    from db import db

    if not os.path.exists(directory+dbname+'.db'):

    mydb = db(directory+dbname+'.db')

    mydb.query(u"create table testing (id counter, name varchar)")
    mydb.query(u"create table carburante (id counter, data text, prezzolitro float, euro float, pagato text, achi text, km float, altra text)")

    mydb.query(u"create table ore (id counter, data text, luogopartenza text, kmpartenza text, orapartenza text, oraarrivo text, luogoarrivo text, kmarrivo text, altra text)")

    mydb.query(u"insert into testing (name) values ('test 1')")

    mydb.query(u"insert into testing (name) values ('test 2')")

    error

    self.affected_rows = self.db.execute(unicode(sql))
    symbian error [errno -6]
    kerrargument

    thanks
    /slupz

  2. #2
    Nokia Developer Moderator
    Join Date
    May 2007
    Location
    21.46 N 72.11 E
    Posts
    3,796

    Re: errors in creating the database

    Pankaj Nathani
    www.croozeus.com

  3. #3
    Registered User
    Join Date
    May 2009
    Location
    italia
    Posts
    9

    Re: errors in creating the database

    Quote Originally Posted by croozeus View Post

    I was using this code I found on the web, what do you think?

    # Filename: db.py save in your Python dir
    import e32db

    class db:
    def __init__(self, dbpath):
    self.db = e32db.Dbms()
    self.dbv = e32db.Db_view()
    self.reset_counters()
    try:
    self.db.open(unicode(dbpath))
    except:
    self.db.create(unicode(dbpath))
    self.db.open(unicode(dbpath))

    def reset_counters(self):
    self.affected_rows = 0
    self.num_rows = 0
    self.__internal_counter = 0

    def query(self, sql):
    self.reset_counters()
    if sql.lower().startswith('select'):
    self.dbv.prepare(self.db, unicode(sql))
    self.dbv.first_line()
    self.num_rows = self.dbv.count_line()
    else:
    self.affected_rows = self.db.execute(unicode(sql))

    def next(self):
    row = {'id': 0}
    if self.num_rows < 1:
    self.reset_counters()
    raise StopIteration
    elif self.__internal_counter < self.num_rows:
    self.dbv.get_line()
    for i in range(self.dbv.col_count()):
    row[i] = self.dbv.col(i+1)
    self.dbv.next_line()
    self.__internal_counter += 1
    return row
    else:
    self.reset_counters()
    raise StopIteration

    def __iter__(self):
    return self

  4. #4
    Nokia Developer Champion
    Join Date
    Feb 2008
    Location
    Ahmedabad, Gujarat, India
    Posts
    3,852

    Re: errors in creating the database

    hello slupz

    it will be great if you edit the post and use the code tags, code tags can be added by adding the clicking the # symbol in your editor tool.

    Code tags keep the indentation and makes the code understandable.

    Regards
    Gaba88

  5. #5
    Registered User
    Join Date
    May 2009
    Location
    italia
    Posts
    9

    Re: errors in creating the database

    Quote Originally Posted by gaba88 View Post
    hello slupz

    it will be great if you edit the post and use the code tags, code tags can be added by adding the clicking the # symbol in your editor tool.

    Code tags keep the indentation and makes the code understandable.

    Regards
    Gaba88
    hellp Gaba88,

    sorry, here is the code with proper indentation.


    Code:
    # Filename: db.py save in your Python dir
    import e32db
    
    class db:
        def __init__(self, dbpath):
    	self.db = e32db.Dbms()
    	self.dbv = e32db.Db_view()
    	self.reset_counters()
    	try:
    	    self.db.open(unicode(dbpath))
    	except:
    	    self.db.create(unicode(dbpath))
    	    self.db.open(unicode(dbpath))
    
        def reset_counters(self):
    	self.affected_rows = 0
    	self.num_rows = 0
    	self.__internal_counter = 0
    
        def query(self, sql):
    	self.reset_counters()
    	if sql.lower().startswith('select'):
    	    self.dbv.prepare(self.db, unicode(sql))
    	    self.dbv.first_line()
    	    self.num_rows = self.dbv.count_line()
    	else:
    	    self.affected_rows = self.db.execute(unicode(sql))
    
        def next(self):
    	row = {'id': 0}
    	if self.num_rows < 1:
    	    self.reset_counters()
    	    raise StopIteration
    	elif self.__internal_counter < self.num_rows:
    	    self.dbv.get_line()
    	    for i in range(self.dbv.col_count()):
    		row[i] = self.dbv.col(i+1)
    	    self.dbv.next_line()
    	    self.__internal_counter += 1
    	    return row
    	else:
    	    self.reset_counters()
    	    raise StopIteration
    
        def __iter__(self):
    	return self

  6. #6
    Regular Contributor
    Join Date
    Sep 2008
    Location
    London, UK
    Posts
    176

    Re: errors in creating the database

    i've noticed database errors too, since i'm developing a type of dictionary app for me to make notes.
    it seems that you can create a database, but to enter values, only two fields are allowed.
    i dont know if this is a bug, or how python handles database queries, but I assume like slupz, would like to be able to use a functioning database with more than two columns!

    *edit:
    perhaps this explains it (http://docs.python.org/library/dbm.html) "Dbm objects behave like mappings (dictionaries), except that keys and values are always strings."
    Last edited by the86hitman; 2009-08-02 at 22:29.

  7. #7
    Registered User
    Join Date
    May 2009
    Location
    italia
    Posts
    9

    Re: errors in creating the database

    Quote Originally Posted by the86hitman View Post
    i've noticed database errors too, since i'm developing a type of dictionary app for me to make notes.
    it seems that you can create a database, but to enter values, only two fields are allowed.
    i dont know if this is a bug, or how python handles database queries, but I assume like slupz, would like to be able to use a functioning database with more than two columns!

    *edit:
    perhaps this explains it (http://docs.python.org/library/dbm.html) "Dbm objects behave like mappings (dictionaries), except that keys and values are always strings."


    exact
    anyone who can help us?

  8. #8
    Regular Contributor
    Join Date
    Sep 2008
    Location
    London, UK
    Posts
    176

    Re: errors in creating the database

    just a brainwave, but perhaps data is better stored as dictionary anyway eg.

    Code:
    we know file.db will store key=value pairs...
    key=1 value={'forename':'john', 'surname':'smith', 'dob':'01/01/1990'}
    key=2 value={'forename':'joe', 'surname':'bloggs', 'dob':'25/12/1995'}
    haven't tested the logistics of this, but there's a small chance it could work?
    The86Hitman
    PyS60 1.9.7 on E71
    http://www.drhtailor.com/pys60

  9. #9
    Regular Contributor
    Join Date
    Sep 2008
    Location
    London, UK
    Posts
    176

    Re: errors in creating the database

    ok, the definitive answer to Databasing in Python after alot of messing around in Python Shell

    Code:
    import e32db
    
    #initiate db access
    db=e32db.Dbms()
    dbv=e32db.Db_view()
    #open database, if not exist, then create the file
    try:
      db.open(u'e:\\data\\python\\test.db')
    except:
      db.create(u'e:\\Python\\src\\test.db')
      db.open(u'e:\\data\\python\\test.db')
    
    #to create ur table
    db.execute("create table table1 (id count,forename varchar, surname varchar)")
    #contains three fields
    
    #to insert data
    db.execute("insert into table values(1,'joe','bloggs')")
    #notice, the 1 is required to increment the key!
    
    #to fetch the whole dataset
    dbv.prepare(db,u'select * from table1')
    
    for i in range(1,dbv.count_line()+1): #1 to number of rows
      dbv.get_line() #grabs the current row
      for i in range(1,self.dbv.col_count()+1):
        print dbv.col(i) #prints each column data
      dbv.next_line() #move to the next rowset
    
    
    
    # use db.execute to perform insert/update/delete 
    # use dbv.prepare to fetch data
    #
    # this is only a framework guide, but you can put this into a class as per your own requirements
    so far, my database is coming along swimmingly.
    for a quick overview of sql statements i use http://www.w3schools.com/php/php_mysql_intro.asp

    any more queries, i'm happy to help
    Last edited by the86hitman; 2009-08-13 at 22:16.
    The86Hitman
    PyS60 1.9.7 on E71
    http://www.drhtailor.com/pys60

  10. #10
    Registered User
    Join Date
    May 2009
    Location
    italia
    Posts
    9

    Re: errors in creating the database

    Quote Originally Posted by the86hitman View Post
    ok, the definitive answer to Databasing in Python after alot of messing around in Python Shell

    Code:
    import e32db
    
    #initiate db access
    db=e32db.Dbms()
    dbv=e32db.Db_view()
    #open database, if not exist, then create the file
    try:
      db.open(u'e:\\data\\python\\test.db')
    except:
      db.create(u'e:\\Python\\src\\test.db')
      db.open(u'e:\\data\\python\\test.db')
    
    #to create ur table
    db.execute("create table table1 (id count,forename varchar, surname varchar)")
    #contains three fields
    
    #to insert data
    db.execute("insert into table values(1,'joe','bloggs')")
    #notice, the 1 is required to increment the key!
    
    #to fetch the whole dataset
    dbv.prepare(db,u'select * from table1')
    
    for i in range(1,dbv.count_line()+1): #1 to number of rows
      dbv.get_line() #grabs the current row
      for i in range(1,self.dbv.col_count()+1):
        print dbv.col(i) #prints each column data
      dbv.next_line() #move to the next rowset
    
    
    
    # use db.execute to perform insert/update/delete 
    # use dbv.prepare to fetch data
    #
    # this is only a framework guide, but you can put this into a class as per your own requirements
    so far, my database is coming along swimmingly.
    for a quick overview of sql statements i use http://www.w3schools.com/php/php_mysql_intro.asp

    any more queries, i'm happy to help



    I still have problems, do not understand why I can not enter data in the database, paste the code

    thanks

    Code:
    db=e32db.Dbms()
    
    dbv=e32db.Db_view()
    
    try:
    
        db.open(u'e:\\Python\\src\\test.db')
    
    except:
    
        db.create(u'e:\\Python\\src\\test.db')
    
        db.open(u'e:\\Python\\src\\test.db')
    
    
    
        #to create ur table
    
        db.execute(u"create table fuel (id counter, date varchar, priceLiter float, euro float, paid varchar, who varchar, km float, another long varchar)")
    
    db.execute(u"insert into fuel values(1,'%s','%f','%f','%s','%s','%f','%s')" % (date, priceLiter, euro, paid, who, km, another))

  11. #11
    Regular Contributor
    Join Date
    Jan 2009
    Location
    India
    Posts
    138

    Re: errors in creating the database

    Hi Slupz,

    Please refer the wiki article Check this link for Complete SQL Database Guide for PyS60

    Also refer this below code with separate db class:
    Code:
    import e32db, os
    
    '''-----------------------------------
    	Python S60 Database Class
    -----------------------------------'''
    class db:
        def __init__(self, dbpath):
    		self.db = e32db.Dbms()
    		self.dbv = e32db.Db_view()
    		self.reset_counters()
    		try:
    		    self.db.open(unicode(dbpath))
    		except:
    		    self.db.create(unicode(dbpath))
    		    self.db.open(unicode(dbpath))
    
        def reset_counters(self):
    		self.affected_rows = 0
    		self.num_rows = 0
    		self.__internal_counter = 0
    
        def query(self, sql):
    		self.reset_counters()
    		if sql.lower().startswith('select'):
    		    self.dbv.prepare(self.db, unicode(sql))
    		    self.dbv.first_line()
    		    self.num_rows = self.dbv.count_line()
    		else:
    		    self.affected_rows = self.db.execute(unicode(sql))
    
        def next(self):
    		row = {'id': 0}
    		if self.num_rows < 1:
    		    self.reset_counters()
    		    raise StopIteration
    		elif self.__internal_counter < self.num_rows:
    		    self.dbv.get_line()
    		    for i in range(self.dbv.col_count()):
    			row[i] = self.dbv.col(i+1)
    		    self.dbv.next_line()
    		    self.__internal_counter += 1
    		    return row
    		else:
    		    self.reset_counters()
    		    raise StopIteration
    
        def __iter__(self):
    		return self
    
    db_nam = "c:\\MyPythonDB.db"
    pydb = db(db_nam)
    #pydb.query("create table testing (id counter, name varchar)") # Need to create table once, after created table make this statement comment
    pydb.query("insert into testing (name) values ('Pavan Pareta')")
    pydb.query('select * from testing')
    for dbrow in pydb:
        print dbrow[0]," | ",dbrow[1]
    Hope this may be help.
    Pavan Pareta
    Sr. Software Engineer
    http://windows-mobile-dev.blogspot.com

  12. #12
    Registered User
    Join Date
    May 2009
    Location
    italia
    Posts
    9

    Re: errors in creating the database

    Quote Originally Posted by pavan.pareta View Post
    Hi Slupz,

    Please refer the wiki article Check this link for Complete SQL Database Guide for PyS60

    Also refer this below code with separate db class:
    Code:
    import e32db, os
    
    '''-----------------------------------
    	Python S60 Database Class
    -----------------------------------'''
    class db:
        def __init__(self, dbpath):
    		self.db = e32db.Dbms()
    		self.dbv = e32db.Db_view()
    		self.reset_counters()
    		try:
    		    self.db.open(unicode(dbpath))
    		except:
    		    self.db.create(unicode(dbpath))
    		    self.db.open(unicode(dbpath))
    
        def reset_counters(self):
    		self.affected_rows = 0
    		self.num_rows = 0
    		self.__internal_counter = 0
    
        def query(self, sql):
    		self.reset_counters()
    		if sql.lower().startswith('select'):
    		    self.dbv.prepare(self.db, unicode(sql))
    		    self.dbv.first_line()
    		    self.num_rows = self.dbv.count_line()
    		else:
    		    self.affected_rows = self.db.execute(unicode(sql))
    
        def next(self):
    		row = {'id': 0}
    		if self.num_rows < 1:
    		    self.reset_counters()
    		    raise StopIteration
    		elif self.__internal_counter < self.num_rows:
    		    self.dbv.get_line()
    		    for i in range(self.dbv.col_count()):
    			row[i] = self.dbv.col(i+1)
    		    self.dbv.next_line()
    		    self.__internal_counter += 1
    		    return row
    		else:
    		    self.reset_counters()
    		    raise StopIteration
    
        def __iter__(self):
    		return self
    
    db_nam = "c:\\MyPythonDB.db"
    pydb = db(db_nam)
    #pydb.query("create table testing (id counter, name varchar)") # Need to create table once, after created table make this statement comment
    pydb.query("insert into testing (name) values ('Pavan Pareta')")
    pydb.query('select * from testing')
    for dbrow in pydb:
        print dbrow[0]," | ",dbrow[1]
    Hope this may be help.
    I followed what he says in this link

    http://wiki.forum.nokia.com/index.ph...uide_for_PyS60

    but I do not understand where mistake in my code because errors in entering data into the database

  13. #13
    Regular Contributor
    Join Date
    Jan 2009
    Location
    India
    Posts
    138

    Thumbs up Re: errors in creating the database

    Quote Originally Posted by slupz View Post
    I followed what he says in this link

    http://wiki.forum.nokia.com/index.ph...uide_for_PyS60

    but I do not understand where mistake in my code because errors in entering data into the database

    Hi Slupz,

    I hope didn't use given class for data base access, because i have twise tested but i am not getting erro,

    how to use given db class in application.

    Code:
    db_nam = "c:\\MyPythonDB.db"
    pydb = db(db_nam)
    
    
    # if database does not have table must create table once
    # after created table make below line comment
    
    # make this statement comment
    pydb.query("create table testing (id counter, name varchar)")
    
    
    pydb.query("insert into testing (name) values ('Pavan Pareta')")
    pydb.query('select * from testing')
    for dbrow in pydb:
        print dbrow[0]," | ",dbrow[1]
    Hope it will work for you.
    Pavan Pareta
    Sr. Software Engineer
    http://windows-mobile-dev.blogspot.com

  14. #14
    Registered User
    Join Date
    May 2009
    Location
    italia
    Posts
    9

    Re: errors in creating the database

    Quote Originally Posted by pavan.pareta View Post
    Hi Slupz,

    I hope didn't use given class for data base access, because i have twise tested but i am not getting erro,

    how to use given db class in application.

    Code:
    db_nam = "c:\\MyPythonDB.db"
    pydb = db(db_nam)
    
    
    # if database does not have table must create table once
    # after created table make below line comment
    
    # make this statement comment
    pydb.query("create table testing (id counter, name varchar)")
    
    
    pydb.query("insert into testing (name) values ('Pavan Pareta')")
    pydb.query('select * from testing')
    for dbrow in pydb:
        print dbrow[0]," | ",dbrow[1]
    Hope it will work for you.

    maybe this code will not work on 5800
    no one can help me?

Similar Threads

  1. Example on Database Creation
    By enjoy_india in forum Symbian
    Replies: 1
    Last Post: 2009-03-03, 13:07
  2. Replies: 2
    Last Post: 2008-02-28, 10:13
  3. problem while creating database
    By ManishPatil in forum Symbian
    Replies: 4
    Last Post: 2003-12-26, 15:33
  4. database problem
    By sridhar_ratna in forum Symbian
    Replies: 1
    Last Post: 2003-04-18, 09:07
  5. Creating and Delete databases
    By -kevbo- in forum Symbian
    Replies: 1
    Last Post: 2001-12-13, 12:54

Posting Permissions

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