Skip to main content
留学咨询

python代写-sqlite3使用案例

By May 15, 2020No Comments

DescriptionYou will be creating a python module that emulates the behaviour of the built-in module, “sqlite3”. Your module will be able to execute SQL statements corresponding to: creating tables, inserting rows, and selecting rows.What you need to doAll of the code you write for this project must be in a folder called “Project2”.Each test case imports “project.py” and expects to find a “connect” function with one parameter. It passes a filename (specifically “test.db”), which isn’t used in this project. The “connect” function should return an object that has two methods “execute” and “close”. For each SQL statement in a test case, the test case will pass that SQL statement as a string to the “execute” method. The method should return an empty list, unless a select statement was executed that yielded rows. In that case, a list of tuples with each denoting a row, should be returned. The “close” method doesn’t do anything, yet.There is no need to write any database output to any files. Persistence may be covered in a later project. Each test will be done on a clean, empty database.Your program is allowed to use any built in modules of python, with the exception of sqlite3. sqlite3 is the reference implementation for this project, your output will be compared against it for correctness. Importing the sqlite3 module in your solution will be considered academic dishonesty for these projects.You may (and should) write additional functions and classes in “project.py” to perform the needed actions.SQL statementsAll SQL keywords will be in ALL_CAPS. For this project, the SQL keywords you need to handle are as follows:CREATE TABLE INSERT INTO VALUES SELECT FROM ORDER BY SQL data types (for this project) and their python equivalents are:SQL Python 3NULL NoneINTEGER intREAL floatTEXT strSQL SyntaxCREATE TABLEExample: CREATE TABLE stocks (symbol TEXT, price REAL, qty INTEGER);Creates a table named “stocks”, with three columns: column “symbol” composed of text strings, column “price” composed of real valued numbers (floating point), and column “qty” composed of integers.INSERT INTOExample: INSERT INTO stocks VALUES (‘GOOG’, 40.4, 7);Inserts the row (‘GOOG’, 40.4, 7), into the table named “stocks”.SELECT *Example: SELECT * FROM stocks ORDER BY price;Returns all the rows in the table named “stocks”. The rows must be ordered by the column “price” ascending. Given the above examples, the result should be a list of tuples: [(‘GOOG’, 40.4, 7)]SELECT columnsExample: SELECT qty, symbol FROM stocks ORDER BY price, qty;Returns all rows, but only showing the qty and symbol values in that order. The rows are sorted by price (and where price is the same, by qty).Test GuaranteesAll tests will be legal SQL (no syntax errors, inserting into nonexistent tables or data type violations). All select statements will have “FROM” and “ORDER BY” clauses.In “CREATE TABLE” statements every attribute will have a data type and no constraints.All table and column names will start with a letter (or underscore) and be followed by 0 or more letters or numbers or underscores.No TEXT will contain single quotes.NULL ValuesSome tests will have NULL values. You need to represent this fact with the python object, None. When returning NULL values from a select statement, you should yield a python None object.ExampleExample Test File of SQL statements (“006: Different Types”):from Project2.project import connect conn = connect(“test.db”)conn.execute(“CREATE TABLE students (col1 INTEGER, col2 TEXT, col3 REAL);”) conn.execute(“INSERT INTO students VALUES (3, ‘hi’, 4.5);”) conn.execute(“INSERT INTO students VALUES (7842, ‘string with spaces’, 3.0);”) conn.execute(“INSERT INTO students VALUES (7, ‘look a null’, NULL);”)result = conn.execute(“SELECT col1, col2, col3 FROM students ORDER BY col1;”) result_list = list(result)expected = [(3, ‘hi’, 4.5), (7, ‘look a null’, None), (7842, ‘string with spaces’, 3.0)]print(“expected:”, expected) print(“student: “, result_list) assert expected == result_listCorrect output from “006: Different Types”:expected: [(3, ‘hi’, 4.5), (7, ‘look a null’, None), (7842, ‘string with spaces’, 3.0)]student: [(3, ‘hi’, 4.5), (7, ‘look a null’, None), (7842, ‘string with spaces’, 3.0)]

admin

Author admin

More posts by admin