Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
pyodbc
#1
A Database was created in SqliteStudio 3.3.3 and is working 100,000 +/- records.

The following python connection string fails :

import pyodbc

con = pyodbc.connect(
    "DRIVER={SQLite3 ODBC Driver};SERVER=localhost;Database=C:\Users\GM\CALL_SIGN_DATABASE1.db;Trusted_connection=yes")
Error message:
Error:
File "C:\Users\Garth Merritt\PycharmProjects\main\SQL.py", line 5 "DRIVER={SQLite3 ODBC Driver};SERVER=localhost;Database=C:\Users\GM\CALL_SIGN_DATABASE1.db;Trusted_connection=yes") ^ SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 57-58: truncated \UXXXXXXXX escape
Is there an error in the connection string ?
Is pyodbc able to connect to SQLiteStudio database ?

Thank you for your comments.
Larz60+ write Feb-20-2022, 12:07 PM:
Please post all code, output and errors (it it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
Fixed for you this time. Please use BBcode tags on future posts.
Reply
#2
Remember that \ followed by a character is considered to be an escape sequence. If you want literal slashes in the string, you need to escape them (i.e. write \\ instead), or use a raw string.
Reply
#3
Also, out if interest, why use this ODBC module to connect to the database, instead of the built-in sqlite3 one?
Reply
#4
I wanted to see what unicode characters are at positions 57-58. To disable treating "\" as part of an escape sequence I used the "r" prefix to make this a "raw" string.
Output:
>>> x = r"DRIVER={SQLite3 ODBC Driver};SERVER=localhost;Database=C:\Users\GM\CALL_SIGN_DATABASE1.db;Trusted_connection=yes" This modifies the str, replacing "\" with "\\". This is how you tell Python that a backslash is just a backslash and not an escape sequence. 'DRIVER={SQLite3 ODBC Driver};SERVER=localhost;Database=C:\\Users\\GM\\CALL_SIGN_DATABASE1.db;Trusted_connection=yes' >>> x[56:] ':\\Users\\GM\\CALL_SIGN_DATABASE1.db;Trusted_connection=yes'
x[57] is the backslash just before "Users".
Reply
#5
(Feb-20-2022, 03:05 PM)deanhystad Wrote: I wanted to see what unicode characters are at positions 57-58. To disable treating "\" as part of an escape sequence I used the "r" prefix to make this a "raw" string.
Output:
>>> x = r"DRIVER={SQLite3 ODBC Driver};SERVER=localhost;Database=C:\Users\GM\CALL_SIGN_DATABASE1.db;Trusted_connection=yes" This modifies the str, replacing "\" with "\\". This is how you tell Python that a backslash is just a backslash and not an escape sequence. 'DRIVER={SQLite3 ODBC Driver};SERVER=localhost;Database=C:\\Users\\GM\\CALL_SIGN_DATABASE1.db;Trusted_connection=yes' >>> x[56:] ':\\Users\\GM\\CALL_SIGN_DATABASE1.db;Trusted_connection=yes'
x[57] is the backslash just before "Users".

Thank you...
Reply
#6
(Feb-20-2022, 12:34 AM)gmerritt Wrote: A Database was created in SqliteStudio 3.3.3 and is working 100,000 +/- records.

The following python connection string fails :

import pyodbc

con = pyodbc.connect(
    "DRIVER={SQLite3 ODBC Driver};SERVER=localhost;Database=C:\Users\GM\CALL_SIGN_DATABASE1.db;Trusted_connection=yes")
Error message:
Error:
File "C:\Users\Garth Merritt\PycharmProjects\main\SQL.py", line 5 "DRIVER={SQLite3 ODBC Driver};SERVER=localhost;Database=C:\Users\GM\CALL_SIGN_DATABASE1.db;Trusted_connection=yes") ^ SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 57-58: truncated \UXXXXXXXX escape
Is there an error in the connection string ?
Is pyodbc able to connect to SQLiteStudio database ?

Thank you for your comments.

Thank you
Reply
#7
(Feb-20-2022, 05:56 AM)ndc85430 Wrote: Also, out if interest, why use this ODBC module to connect to the database, instead of the built-in sqlite3 one?

I have a working version using Sqlite3, but was wondering how the SQL query execution time would vary using an ODBC connection.
Reply
#8
[quote="gmerritt" pid='153743' dateline='1645452287']
[quote="deanhystad" pid='153693' dateline='1645369513']
import pyodbc
import pandas as pd
from sqlalchemy import create_engine

CONN = pyodbc.connect(
    "DRIVER={SQLite3 ODBC Driver};SERVER=localhost;DATABASE=C:\\Users\\DB\\Desktop\\CALLDB.db;Trusted_connection=yes")
cnx = create_engine('sqlite:///CALLDB.db').connect()
# table named 'CALL' will be returned as a dataframe.
df = pd.read_sql_table('CALL', cnx)
print(df)
Thank you for the feedback on the syntax in the connection string. The above code works now.
Reply
#9
#  print in a grid
import pandas as pd
from sqlalchemy import create_engine

# SQLAlchemy connectable
cnx = create_engine('sqlite:///CALLDB.db').connect()

# table named 'contacts' will be returned as a dataframe.
df = pd.read_sql_table('CALL', cnx)
print(df)
The above connection string works well.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Using pyodbc&pandas to load a Table data to df tester_V 3 830 Sep-09-2023, 08:55 PM
Last Post: tester_V
  Formatting Data/Time with Pyodbc and openpyxl bearcats6001 0 2,295 Aug-17-2020, 03:44 PM
Last Post: bearcats6001
  Get database used data space from pyodbc susja 1 2,261 Aug-14-2020, 02:01 PM
Last Post: susja
  pyodbc error ('82', '[82] 523 80 (0) (SQLDriverConnect)') paulsuk1982 1 2,183 Nov-29-2019, 11:05 AM
Last Post: Larz60+
  pyodbc.Error SQLBindParameter pcarra 0 3,920 Jul-08-2019, 08:22 PM
Last Post: pcarra
  Pyodbc error taxit 1 5,213 Jun-18-2019, 01:13 AM
Last Post: Larz60+
  Using VBA to Call a Python script causes error in pyodbc connector pcarra 1 2,843 Jun-11-2019, 04:14 PM
Last Post: pcarra
  Split List and Sublist from Pyodbc parthi1705 1 2,242 May-05-2019, 10:44 AM
Last Post: Larz60+
  Problem with pyodbc executemany() RBeck22 1 7,430 Apr-02-2019, 06:12 PM
Last Post: micseydel
  PyODBC error - second parameter to executemany must be a sequence, iterator, or gener RBeck22 1 7,107 Mar-29-2019, 06:44 PM
Last Post: RBeck22

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020