Feeds:
Posts
Comments

Posts Tagged ‘python web module’

Python has inbulit sqlite3 db..This below program will create a table in sqlite3 and insert a record and will retrieve the data.We use Web.py module to display the data in a html file.Web.py module has inbuilt server which will deploy this html..

Please install this webpy module

Note: User have to align the below code..

SmsAppMain.py

#imports the web.py module
import web
#Import inbulit sqlite db
import sqlite3

#Creating the connection
global connection
connection = sqlite3.connect(‘smsapp.db’)
#Load the cursor
global cursor
cursor = connection.cursor()

def createUser():
#Create the usertbl
cursor.execute(”’create table usertbl(username text,passwd text)”’)
print ‘Created Usertbl’
# Insert a row of data
cursor.execute(“””insert into usertbl values (‘viggi’,’test’)”””)
# Save the changes
connection.commit()

def retrieveUser(name):
import sqlite3 as db
db = web.database(dbn=”sqlite”,db=”smsapp.db”)
#Get a reference to the connection object via the cursor
connection = db._db_cursor().connection
cursor.execute(‘select * from usertbl’)
for element in cursor:
strUser = element
return strUser

#look for templates in your templates directory
render = web.template.render(‘templates/’)

#Regex that matches a URL,like /,/help
urls = (“/.*”, “SmsAppMain”)

#Class with GET function
class SmsAppMain:
def GET(self):
name = ‘Viggi’
#createUser()
strUser = retrieveUser(name)
#index is the name of the template
return render.index(str(strUser))

if __name__ == “__main__”:
#Create an application with the URLs
#looking up the classes in the global namespace
app = web.application(urls, globals())
app.run()

index.htm:

$def with (name)
$if name:
hello to $name.
$else:
Hello, dumb

Create the templates folder and copy this index.htm…
cmd : python SmsAppMain.py

Open a browser and hit this url http://localhost:8080/index.htm


References :

http://webpy.org/skeleton/0.3
http://docs.python.org/library/sqlite3.html

Read Full Post »