OCCI - Connecting to a remote database - c++

I'm fairly new to Oracle. I'm trying to connect to a remote Oracle database using OCCI. All the examples i've found up this point have been for connecting to a local database. Could someone please point me in the right direction and let me know where i can find an example connection to get me past this point? Thanks, Mike

createConnection( "name", "passwrd", "string")
"string" stands either for the connection name that is resolved with the Oracle "tnsnames.ora" file which should be located in your ORACLE_HOME(Oracle install dir)\NETWORK\ADMIN directory or for a connection string like below
Code:
connection_name =
(DESCRIPTION =
(ADDRESS=(PROTOCOL = TCP)(HOST = ip_address)(PORT = listener_port))
(CONNECT_DATA= (SERVICE_NAME = listener_service_name)
(SERVER = DEDICATED))
)

Related

Oracle Connection with SQLAPI++

I'm trying to connect my c++ code with oracle using SQLAPI++ but it gives me the error
TNS:could not resolve the connect identifier specified
My code is like so:
con.Connect( "(DESCRIPTION=(ADDRESS=(PROTOCOL=tcp) (HOST=192.168.1.XXX) (PORT=1521))(CONNECT_DATA = (SERVICE_NAME = SERVICE_NAME )))", "UsrName", "Password", SA_Oracle_Client );
if I only use the same code but the username and password are equal to nothing like this:
con.Connect( "(DESCRIPTION=(ADDRESS=(PROTOCOL=tcp) (HOST=192.168.1.XXX) (PORT=1521))(CONNECT_DATA = (SERVICE_NAME = SERVICE_NAME )))", "", "", SA_Oracle_Client );
the compiler gives me an ORA-01017: Invalid username / password; connection refused\n error.
You may give instant client syntax a try:
[//]host[:port][/service name]
This I've used with SQLAPI++ successfully.
The string you've used is expected to occur in tnsnames.ora and you refer to this entry via a so called tnsname. If you can't change the tnsnames.ora stored in Oracle installation you're using then you can instruct the Oracle API to pick the file up form another location by defining the environmanet varable TNS_ADMIN. Set this to a directory were your own tnsnames.ora file exists.

Connecting to google cloud SQL Error using Flask-SQLAlchemy

I'm totally beginner for use flask and Google Cloud SQL. I want to make Login and Registration in my app using Flask-Login and Flask-SQLAlchemy.
But I can't connect to google cloud SQL using Flask-SQLAlchemy, this is my code.
SQLALCHEMY_DATABASE_URI = (
'mysql+pymysql://{db_user}:{db_password}#/{db_name}'
'?unix_socket=/cloudsql/{connection_name}').format(
user=db_user, password=db_password,
database=db_name, connection_name=connection_name)
app.config['SQLALCHEMY_DATABASE_URI'] = SQLALCHEMY_DATABASE_URI
I just got this error :
(2003, "Can't connect to MySQL server on 'localhost' ([Errno 2] No such file or directory)")
But, when I try to make a connection using SQLAlchemy with this code, the connection is succeeded :
query_string = dict({"unix_socket": "/cloudsql/{}".format(connection_name)})
driver_name = 'mysql+pymysql'
db = sqlalchemy.create_engine(
sqlalchemy.engine.url.URL(
drivername=driver_name,
username=db_user,
password=db_password,
database=db_name,
query=query_string),
pool_size=5,
max_overflow=2,
pool_timeout=30,
pool_recycle=1800)
But when I tried to change the code like this :
app.config['SQLALCHEMY_DATABASE_URI'] = db
then, I got this error: 'Engine' object has no attribute 'drivername'.
Any suggestion for this problem? any advice, might be very helpful for me, Thank you.
This line right here actually assigns the "engine" object to your URI config:
app.config['SQLALCHEMY_DATABASE_URI'] = db
You can use the url object to create a URI instead:
uri = sqlalchemy.engine.url.URL(
drivername=driver_name,
username=db_user,
password=db_password,
database=db_name,
query=query_string)
app.config['SQLALCHEMY_DATABASE_URI'] = uri.render_as_string(hide_password=false)
You can compare the URI to the one you created and see where they differ. It's likely there is a typo, or that some value is escaped incorrectly.

Python Sqlite3 attaching to a memory database

I am using Python 2.7 with sqlite3 version 2.6.0. I am trying to create a memory database, attach to it from a physical database and insert data, then query it back later. I am having issues if anyone can help.
The following two fail with the error message "unable to open database file"
con = sqlite3.connect(":memory:?cache=shared")
con = sqlite3.connect("file::memory:?cache=shared")
The following works until I attempt to access a table in the attached DB. I can do this with physical databases with no problem. I suspect the issue is not having the cache=shared.
con = sqlite3.connect(":memory:")
cursor = con.cursor()
cursor.executescript("create table table1 (columna int)")
cursor.execute("select * from table1")
con2 = sqlite3.connect("anotherdb.db")
cursor2 = con2.cursor()
cursor2.execute("attach database ':memory:' as 'foo'")
cursor2.execute("select * from foo.table1")
The error from the last select is "no such table: foo.table1".
Thanks in advance.
The SQLite library shipped with Python 2.x does not have URI file names enabled, so it is not possible to open an in-memory database in shared-cache mode.
You should switch to apsw, or Python 3.

How can I get around Name too long for LOGINREC in pymssql without changing the username

I am attempting to use pymssql to connect to a client's database, but have thus far been unable to succeed. I believe that the root of the problem is this entry in the stack trace of my TDSDUMP:
dblib.c:761:dbsetlname(0x1ac3e10, <username>#<servername>.database.windows.net, 2)
dblib.c:7929:dbperror((nil), 20042, 0)
dblib.c:7981:20042: "Name too long for LOGINREC field"
One problem is that <servername>.database.windows.net already exceeds the string limit (which seems to be 30 according to this: How to use PHP's dblib PDO driver with long usernames? / SQLSTATE[HY000] Name too long for LOGINREC field (severity 2)).
I have also attempted to exclude the #<servername>.database.windows.net portion in the username entry only to receive the following error:
msgno 40532: "Cannot open server "1433D" requested by the login. The login failed."
According to https://github.com/pymssql/pymssql/issues/330 the #<servername> portion is requested by the server.
So at this point, I attempted to do the following:
user = username + "#{}".format(server)
user = user[:30]
And I received the following information (which was a slight improvement but still not ideal, given that I could still not establish the connection):
"Cannot open server "<server_name minus the last 6 characters>" requested by the login. The login failed."
If possible, I would prefer passing some parameter to pymssql's connect method that would override this character limit or do something to append the server to the username on the backend after calling dbsetlname on just <username> without the #<servername> portion. Does anyone have any recommendations (again the preference is to not ask the client to change our username, but we may have to resort to that or trying to use pyodbc if there's no other option).
These are a few of the connection methods I have tried:
conn = pymssql.connect(
host=host,
database=database,
user=username,
password=password,
port=int(port) # this is 1433
)
conn = pymssql.connect(
host=host,
database=database,
user=username + '#{}'.format(servername),
password=password,
port=int(port) # this is 1433
)
conn = pymssql.connect(
server=<servername>.database.windows.net,
database=database,
user=username,
password=password,
port=int(port) # this is 1433
)
Thanks in advance for any help/advice that you may be able to offer!
You can probably use an alternate connecting string method.
If you're using SQL Server Auth, try this:
conn = pymssql.connect(
server="<servername>.database.windows.net",
port=1433,
user="username",
password="password",
database="dbname"
)
If you're using Windows Auth:
conn = pymssql.connect(
server="<servername>.database.windows.net",
port=1433,
user="DOMAIN\USERNAME",
password="password",
database="dbname"
)
Have you tried either of these connect methods?

Connect Python to H2

I'm trying to make a connection from python2.7 to H2 (h2-1.4.193.jar - latest)
H2 (is running and available): java -Dh2.bindAddress=127.0.0.1 -cp "E:\Dir\h2-1.4.193.jar;%H2DRIVERS%;%CLASSPATH%" org.h2.tools.Server -tcpPort 15081 -baseDir E:\Dir\db
For python I'm using jaydebeapi:
import jaydebeapi
conn = jaydebeapi.connect('org.h2.Driver', ['jdbc:h2:tcp://localhost:15081/db/test', 'sa', ''], 'E:\Path\to\h2-1.4.193.jar')
curs = conn.cursor()
curs.execute('create table PERSON ("PERSON_ID" INTEGER not null, "NAME" VARCHAR not null, primary key ("PERSON_ID"))')
curs.execute("insert into PERSON values (1, 'John')")
curs.execute("select * from PERSON")
data = curs.fetchall()
print(data)
As a result everytime I get an error: Process finished with exit code -1073741819 (0xC0000005)
Do you have any ideas about this case? Or maybe there is something else that I can use instead of the jaydebeapi?
Answering my own question:
First of all I could not do anything through the jaydebeapi.
I've read that H2 supports PostgreSQL network protocol. My next steps were to transfer h2 and python into pgsql:
H2 pg:
java -Dh2.bindAddress=127.0.0.1 -cp h2.jar;postgresql-9.4.1212.jre6.jar org.h2.tools.Server -baseDir E:\Dir\h2\db
TCP server running at tcp://localhost:9092 (only local connections)
PG server running at pg://localhost:5435 (only local connections)
Web Console server running at http://localhost:8082 (only local connections)
postgresql.jar was included to try to connect from Web Console.
Python: psycopg2 instead of jaydebeapi:
import psycopg2
conn = psycopg2.connect("dbname=h2pg user=sa password='sa' host=localhost port=5435")
cur = conn.cursor()
cur.execute('create table PERSON ("PERSON_ID" INTEGER not null, "NAME" VARCHAR not null, primary key ("PERSON_ID"))')
As a result - it's working now. Connection was established and table was created.
Web Console settings:
Generic PostgreSQL
org.postgresql.Driver
jdbc:postgresql://localhost:5435/h2pg
name: sa, pass: sa
Web Console did connect but did not show me table list and showed many errors instead: "CURRENT_SCHEMAS" is not found etc.... PG admin 4 was not also able to connect. SQuirrel to the rescue - it had connected to this db and all is working fine there.
Perhaps a bit late for an update after 1.5 years, but the current version connects fine with H2, without having to use a postgres driver.
conn = jaydebeapi.connect("org.h2.Driver", "jdbc:h2:~/test", ["sa", ""], "/Users/angelo/websites/GEPR/h2/bin/h2-1.4.197.jar",)
source: https://pypi.org/project/JayDeBeApi/#usage