Sqlite3 Vs web2py DAL

415 views Asked by At

I have been working with sqlite DB for some time but want to integrate my codes to web2py esp. DAL. How do I rewrite such a code to web2py DAL code?

      name = input ('Please Type your Question:  ').lower().split()  
      name2 = name[:]
      import sqlite3
      for item in name2:#break                
         conn = sqlite3.connect("foods.db")
         cursor = conn.cursor()           
         cursor.execute("INSERT INTO INPUT33 (NAME) VALUES (?);", (name2,))      
         cursor.execute("select MAX(rowid) from [input33];")
         conn.commit()      
         for rowid in cursor:break         
         for elem in rowid:
            m = elem            
            print(m)
            cursor.execute("DELETE FROM INPUT33 (NAME) WHERE NAME = name")
1

There are 1 answers

0
Diogo Martins On BEST ANSWER

I do not quite understand the question so I would like to apologize in advance for any misunderstanding.

Web2py is a Web MVC framework and you should follow that pattern while designing your application. Having that in mind, using a console-related function like input makes no sense. Also, you shouldn't use the same component to extract user interaction related data and deal with database connection and data access/manipulation.

If your intent is to simply convert your code snippet that used sqlite3 module into using pyDAL you just need to install it pip install pydal and change your code to something like

#Do your imports
from pydal import DAL, Field

#  connect to your database
db = DAL('sqlite://foods.db')
# define your table model and table fields
db.define_table('input33', Field('NAME'))

# perform an insert into input database
db.input33.insert(name=name2)
# every insert/delete/update needs your to commit to your changes
db.commit() 

A full documentation can be found here