第一次用python构建一个简单的应用程序

第一次用python构建一个简单的应用程序,python,sql,database,cx-oracle,Python,Sql,Database,Cx Oracle,我是一名本科生,这是我第一次为数据库构建一个非常简单的应用程序 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() # C

我是一名本科生,这是我第一次为数据库构建一个非常简单的应用程序

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()
以上这些代码是我所知道的关于如何连接数据库和基本上运行一些SQL查询的全部内容

我们小组的成员都是编程新手,所以开发应用程序对我们来说是新的。 这个项目需要5个部分。我想向您展示5个部分中的一个,不想让您为我编写代码,我想要一些提示/提示。一旦收到任何有用的提示并更新我的流程,我将在线等待帮助并使用该程序

新车登记
该组件用于自动登记官员登记新车。新车辆是指尚未在数据库中注册的车辆。部件应允许官员输入车辆的详细信息和新车主的个人信息(如果不在数据库中)

这完全取决于您希望如何接收数据。如果这是一个交互式的东西,只需使用
raw\u input
(使用python 2.x),然后使用字符串格式将它们放入查询中:

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()
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)

当然,您必须自己进行所有验证,并且-记住要安全(即,即使有密码保护,检查SQL注入的输入仍然是一个好主意)。

我的假设是,我们是否应该使用一些输入部分来输入数据?这取决于您希望如何接收数据。如果一次只能输入一个数据,那么编写一个希望以交互方式输入数据的程序没有什么错。但是,如果您想一次输入一组数据,最好编写一个脚本,从某种文件(csv、excel、XML等)上传信息。例如,如果官员想输入数据,我的语句可能是“插入车辆值('ABC123',null,'SUV','Jack')。在我们的程序中,我们一次只需要插入一个。我知道oracledeveloper如何做到这一点,但通过python在本例中@Yuvit这一部分很有意义,我正在开始并尝试构建一个简单的UI。感谢you@SonicFancy如果你发现这有帮助,考虑把它标记为正确的。在使用progra时,可能还有一些问题m@SonicFancy你可以编辑你的问题,我保证我会继续帮助你,但只限于某一点。所以这不像是一个论坛,所以对于你遇到的每一个问题,首先试着看看以前是否有人问过(很可能是问过),如果你仍然找不到任何问题,打开一个新的问题(特别是如果它与你当前的问题不太相关)