Rental Bicycle App: Write/Read Data

ក្រោយ​ពី​ទិន្នន័យ​ទទួល​បាន​មក​ពី form ត្រូវ​បាន​ត្រួត​ពិនិត្យ​ចំនួន ២ ដង​រួច​មក យើង​អាច​បញ្ចូល​ទិន្នន័យ​ទាំងនោះ​ចូល​ទៅ​ក្នុង​មូលដ្ឋាន​ទិន្នន័យ SQLite របស់​យើង​បាន ដោយ​សរសេរ​កូដ​ដូច​ខាង​ក្រោម​នេះ៖

#\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()
#\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 មក​សរសេរ​បង្ហាញ​នៅ​លើ​ទំព័រ​មុខ​ យើង​អាច​សរសេរកូដ​ដូច​ខាង​ក្រោម​នេះ៖

#\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
#\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)


%include("./partials/header.tpl")



BICYCLES
%include("./partials/sidebar.tpl")
%include("./partials/footer")
//\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/