ក្រោយពីទិន្នន័យទទួលបានមកពី form ត្រូវបានត្រួតពិនិត្យចំនួន ២ ដងរួចមក យើងអាចបញ្ចូលទិន្នន័យទាំងនោះចូលទៅក្នុងមូលដ្ឋានទិន្នន័យ SQLite របស់យើងបាន ដោយសរសេរកូដដូចខាងក្រោមនេះ៖
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | #\models\insertBicycle.py import sqlite3 def insert( * bicycle): conn = sqlite3.connect( 'sqlite' ) cursor = conn.cursor() sql = '''CREATE TABLE IF NOT EXISTS BICYCLE( BRAND TEXT, COUNTRY TEXT, YEAR INT, AMOUNT INT, PRICE FLOAT ) ''' cursor.execute(sql) cursor.execute( "INSERT INTO BICYCLE VALUES (?, ?, ?, ?, ?)" , bicycle) conn.commit() conn.close() |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | #\controllers\bikeform.py import config, re from bottle import template, route, request from models import insertBicycle @route ( "/bikeform" ) def renderForm(): return template( 'bikeform' , data = config.kargs) @route ( "/bikeform" , method = "POST" ) def getFormData(): brand = request.forms.get( "fbrand" ) country = request.forms.get( "fcountry" ) year = request.forms.get( "fyear" ) amount = request.forms.get( "famount" ) price = request.forms.get( "fprice" ) if not re.findall( "[a-zA-Z]" , country): config.kargs[ 'message' ] = "Country name could contain only letter." return template( 'bikeform' , data = config.kargs) elif not (re.findall( "[0-9]" , year) and re.findall( "[0-9]" , amount)): config.kargs[ 'message' ] = "Year and amount must be whole number." return template( 'bikeform' , data = config.kargs) elif not (re.findall(r "[-+]?\d*\.\d+|\d+" , price)): config.kargs[ 'message' ] = "Price must be a number." return template( 'bikeform' , data = config.kargs) else : insertBicycle.insert(brand, country, int (year), int (amount), float (price)) return template( 'bikeform' , data = config.kargs) |
ចំពោះការស្រង់យកទិន្នន័យដែលត្រូវបានបញ្ចូលទៅក្នុងមូលដ្ឋានទិន្នន័យ SQLite មកសរសេរបង្ហាញនៅលើទំព័រមុខ យើងអាចសរសេរកូដដូចខាងក្រោមនេះ៖
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | #\models\selectBicycle.py import sqlite3 def select(): conn = sqlite3.connect( 'sqlite' ) cursor = conn.cursor() sql = '''CREATE TABLE IF NOT EXISTS BICYCLE( BRAND TEXT, COUNTRY TEXT, YEAR INT, AMOUNT INT, PRICE FLOAT ) ''' cursor.execute(sql) cursor.execute( "SELECT * from BICYCLE" ) bicycles = cursor.fetchall() conn.commit() conn.close() return bicycles |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | #\app.py import os, json, config from bottle import route, run from controllers import index, bikeform from public import setup from models import selectBicycle @route ( '/' ) def main(): config.kargs[ 'bicycles' ] = json.dumps(selectBicycle.select()) return index.render(config.kargs) if 'DYNO' in os.environ: run(host = '0.0.0.0' , port = os.environ.get( 'PORT' , 9000 )) else : run(host = 'localhost' , port = 9000 , debug = True , reloader = True ) |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | <!--\views\index.tpl--> %include("./partials/header.tpl") < style > </ style > < div class = "main" id = "main" > < div class = "content" id = "content" > < div class = "top-widget" > < span >BICYCLES</ span >< input onclick = "location.href='/bikeform'" type = "button" value = "Add Bicycle" > </ div > < div class = "bottom-widget" > < span > < select > < option value = "Brand" >Brand</ option > </ select > < input type = "button" value = "Sort" > </ span > < span class = "search" > < input type = "text" >< input type = "button" value = "Search" > </ span > </ div > <!--bottom-widget--> < div id = "table" ></ div > <script> bicycle.showBicycle({{!data[ 'bicycles' ]}}) </script> </ div > <!--content--> %include("./partials/sidebar.tpl") </ div > <!--main--> %include("./partials/footer") |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 | //\public\js\main.js class Bicycle{ bicycleForm(formId){ var brand = document.forms[formId][ 'fbrand' ].value; var country = document.forms[formId][ 'fcountry' ].value; var year = document.forms[formId][ 'fyear' ].value; var amount = document.forms[formId][ 'famount' ].value; var price = document.forms[formId][ 'fprice' ].value; if ((brand == "" ) || (country == "" ) || (year == "" ) || (amount == "" ) || (price == "" )){ return false ; } else { var numberRGEX = /^(?:[1-9]\d*|0)?(?:\.\d+)?$/; var intRGEX = /^[0-9]+$/; var numberResult = numberRGEX.test(price); var intResult = (intRGEX.test(year) && intRGEX.test(amount)); if (!numberResult){ alert( 'Please enter a number for price.' ); return false ; } if (!intResult){ alert( 'Please enter a whole number for year and amount.' ); return false ; } } } showBicycle(bicycles){ var html = '' ; if (bicycles.length > 0){ html += "<table>" ; html += "<tr>" ; for ( var k in {id:0, brand:0, country:0, year:0, amount:0, price:0}){ html += "<th>" + k.toUpperCase() + "</th>" ; } html += "<th>OPTION</th>" ; html += "</tr>" ; for ( var i=0; i<bicycles.length; i++){ html += "<tr>" ; html += "<td>" + (i+1) + "</td>" ; for ( var j in bicycles[i]){ html += "<td>" + bicycles[i][j] + "</td>" ; } html += `<td class= "option" ><a onclick= "alert(${i+1},'bikeform')" class= "edit" id= "'+i+'" >Edit</a>|<a onclick= "alert(${i+1})" class= "delete" >Delete</a></td>`; html += "</tr>" ; } html += "</table>" ; } document.getElementById( "table" ).innerHTML = html; } } //end of class var bicycle = new Bicycle(); |
GitHub: https://github.com/Sokhavuth/Rental-Bicycle-App
Heroku: https://khmerweb-rba.herokuapp.com/