Created
November 3, 2021 11:34
-
-
Save ozcanyarimdunya/3b4e44d5492046b00188f48242a07228 to your computer and use it in GitHub Desktop.
Connect database via jaydebeapi
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pathlib | |
import jaydebeapi as jdbc | |
# Create a dir 'jars' and download related jar files | |
jars = pathlib.Path(__file__).parent.joinpath("jars") | |
databases = [ | |
{ | |
"classname": "org.sqlite.JDBC", | |
"url": "jdbc:sqlite::memory:" | |
}, | |
{ | |
"classname": "ru.yandex.clickhouse.ClickHouseDriver", | |
"url": "jdbc:clickhouse://localhost:8123" | |
}, | |
] | |
def connection(classname, url): | |
try: | |
with jdbc.connect( | |
jclassname=classname, | |
url=url, | |
jars=[i.as_posix() for i in jars.glob("*.jar")], | |
) as conn: | |
cursor = conn.cursor() | |
cursor.execute("select 2;") | |
result, = cursor.fetchone() | |
assert result == 2, result | |
print("[SUCCESS]") | |
except Exception as ex: | |
print("[ERROR]: {}".format(ex)) | |
for each in databases: | |
connection(**each) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment