Вопрос

I am an undergraduate student and this is my first time to build a pretty simple app for database.

import cx_Oracle
import getpass

# Get username and password for connecting to the database
user = input("Username [%s]:" % getpass.getuser())
if not user:
    user = getpass.getuser()
password = getpass.getpass()

# Connecting to the database by the following format
conString = user + "/" + password + "@XXXXXXX"
connection = cx_Oracle.connect(conString)

cursors = connection.cursor()

# Enter the SQL statement 
stmt = "xxxxxxxxxx"
# Run the statement
cursors.execute(stmt)

# Enter and run the SQL query
query = "SELECT xxxx, FROM xxxx"
cursors.execute(query)

rows = cursors.fetchall()
for row in rows:
print(row)

# IMPORTANT: Close the cursors and connection
curs.close()
con.close()

These code above is all I know abt how to connect the database and basically run some SQL query.

We group members are all beginners in programming so it is new to us to build an app. This project require 5 parts. I want to show you one of 5 parts and do not want you to write the code for me, I want some tips/hints. I will be online to wait for the help and work with the program once receive any useful tips and update my process.

New Vehicle Registration This component is used to register a new vehicle by an auto registration officer. By a new vehicle, we mean a vehicle that has not been registered in the database. The component shall allow an officer to enter the detailed information about the vehicle and personal information about its new owners, if it is not in the database.

Это было полезно?

Решение

It all depends on how you expect to receive the data. If this is an interactive thing, just use raw_input (with python 2.x) and then put them into queries using string formatting:

license_number = raw_input("please insert the vehicels license number: ")
car_type = raw_input("what car type? ")
# etc.

# validate inputs here.
# note that with python 2.x the variables will always be strings
# if using python 3.x, use input() instead of raw_input()

# put them into a query using string formatting
query = "INSERT INTO VEHICLE VALUES('%s','%s','%s','%s')" % (license_number, 'null', car_type, owner_name)

Of course, you're going to have to do all validation yourself, and also - remember to be secure (i.e., even with password protection, it's still a good idea to check the input for SQL injection).

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top