mirror of
https://github.com/python/cpython.git
synced 2025-08-03 16:39:00 +00:00
[3.10] gh-95273: Improve sqlite3.complete_statement docs (GH-95840) (#95918)
Co-authored-by: Ezio Melotti <ezio.melotti@gmail.com>
Co-authored-by: CAM Gerlach <CAM.Gerlach@Gerlach.CAM>.
(cherry picked from commit e6623e7083
)
Co-authored-by: Erlend E. Aasland <erlend.aasland@protonmail.com>
This commit is contained in:
parent
bfaa071e1c
commit
75299dcab5
2 changed files with 14 additions and 36 deletions
|
@ -1,30 +0,0 @@
|
|||
# A minimal SQLite shell for experiments
|
||||
|
||||
import sqlite3
|
||||
|
||||
con = sqlite3.connect(":memory:")
|
||||
con.isolation_level = None
|
||||
cur = con.cursor()
|
||||
|
||||
buffer = ""
|
||||
|
||||
print("Enter your SQL commands to execute in sqlite3.")
|
||||
print("Enter a blank line to exit.")
|
||||
|
||||
while True:
|
||||
line = input()
|
||||
if line == "":
|
||||
break
|
||||
buffer += line
|
||||
if sqlite3.complete_statement(buffer):
|
||||
try:
|
||||
buffer = buffer.strip()
|
||||
cur.execute(buffer)
|
||||
|
||||
if buffer.lstrip().upper().startswith("SELECT"):
|
||||
print(cur.fetchall())
|
||||
except sqlite3.Error as e:
|
||||
print("An error occurred:", e.args[0])
|
||||
buffer = ""
|
||||
|
||||
con.close()
|
|
@ -222,14 +222,22 @@ Module functions
|
|||
|
||||
.. function:: complete_statement(statement)
|
||||
|
||||
Returns ``True`` if the string *statement* contains one or more complete SQL
|
||||
statements terminated by semicolons. It does not verify that the SQL is
|
||||
syntactically correct, only that there are no unclosed string literals and the
|
||||
statement is terminated by a semicolon.
|
||||
Return ``True`` if the string *statement* appears to contain
|
||||
one or more complete SQL statements.
|
||||
No syntactic verification or parsing of any kind is performed,
|
||||
other than checking that there are no unclosed string literals
|
||||
and the statement is terminated by a semicolon.
|
||||
|
||||
This can be used to build a shell for SQLite, as in the following example:
|
||||
For example::
|
||||
|
||||
.. literalinclude:: ../includes/sqlite3/complete_statement.py
|
||||
>>> sqlite3.complete_statement("SELECT foo FROM bar;")
|
||||
True
|
||||
>>> sqlite3.complete_statement("SELECT foo")
|
||||
False
|
||||
|
||||
This function may be useful during command-line input
|
||||
to determine if the entered text seems to form a complete SQL statement,
|
||||
or if additional input is needed before calling :meth:`~Cursor.execute`.
|
||||
|
||||
.. function:: enable_callback_tracebacks(flag, /)
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue