Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
pyPgSQL Examples By: Benjamin Arai & Conley Read Summary Code Snippet Insertion Example Deletion Example Selection Example Code Snippet This code snippet is an example of creating a connection and an execution of a query using pyPgSQL. The connection manager function can specify other parameters, which include username and password. All of the source code and queries are assuming the database is PostgreSQL 7.4.1 and Python is version 2.3.3. The pyPgSQL package is not currently installed on any of the lab computers, but is installed on hebe.cs.ucr.edu. from pyPgSQL import PgSQL cx = PgSQL.connect(database=”db_name”) cx.execute(“QUERY”) cx.close() The connection example does not cover SQL commands you would need to do in-order for locking or transactions on the database but it is in most cases it is usually only a additional two or three executions of starting and stopping the task. Insertion Example This is an insertion into the QuestionVersions table and assumes there is no foreign linking between tables. There are also ALTER queries that can be used to modify data in the database. “Integer”, “Integer”, “Integer”, “Boolean”, “Text”, “Text”, “Text”, “Text”, “Text”, “Double”, “Text” ); INSERT INTO QuestionVersions VALUES( Integer Boolean = 32-bit integer number. = Can be represented as a 0/1 or true/false value. Text Double = Text string of variable size. = Double precision number. Deletion Example This is an example of a deletion using the ProfessorID as the only parameter for deletion. The parameter specified for the column most correspond with the type specified in the database. DELETE FROM QuestionVersions WHERE ProfessorID=”Integer”; The ProfessorID is the column from which the query will make the decision on what to delete. Multiple columns can be used after the WHERE CLAUSE to refine and/or widen a search. Selection Example This is a select using the Statement field to find all of the questions that contain the word “tree”. SELECT * FROM QuestionVersions WHERE Statement LIKE %Text%; The “%” sign is a wild card for 0 or more items. The “%” sign can be replaced with a “$”, which represents exactly one character wild card value and can be specified multiple times in a query. Other Stuff These are all simple examples that can be extended to include multiple columns, sub queries and etc. Advanced topics including server-side includes and triggers are covered in the PostgreSQL documentation.