Connection to SQLite3 Python



  • Hi, never dealt with Python, but there's a need to write a code on Python with OBD connections.

    Here's the code.

    import sqlite3  
    conn = sqlite3.connect('test.db')
    print "Opened database successfully";
    

    I understand it's a connection to the already established OBD system, and if it doesn't exist, it's gonna be created.

    Question is whether there is a method that checks whether there is an existing OBD to which we have indicated the connection and how and where to indicate the path to OBD rather than connecting. current directory



  • Write an absolute way to the file and it won't be in you. current directory

    import sqlite3
    

    sqlite_file = '/Users/Test/Desktop/my_db.sqlite'
    conn = sqlite3.connect(sqlite_file)

    Determination of the presence/disclosure of the OBD file by means of a function https://docs.python.org/3/library/os.path.html#os.path.exists

    Return True if path refers to an existing path or an open file descriptor. Returns False for broken symbolic links. On some platforms, this function may return False if permission is not granted to execute os.stat() on the requested file, even if the path physically exists.

    or by assistance https://docs.python.org/3/library/os.path.html#os.path.isfile

    Return True if path is an existing regular file. This follows symbolic links, so both islink() and isfile() can be true for the same path.

    import os.path

    if os.path.exists(sqlite_file):
    print('File exists')
    else:
    print('File NOT exists')




Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2