pyodbc - add column to MS Access with default value - python-3.x

I'm trying to add a column to an MS Access database table using pyodbc and Python 3.5.
Using the expression
self.cursor.execute("ALTER TABLE data ADD COLUMN testColumn TEXT(10)")
works fine, but when I try to add a default value (DEFAULT "no"), it throws a Syntax error. I've tried multiple combinations, but no luck.
Any help is much appreciated!
Cheers

Sadly, the Access ODBC driver simply does not support the DEFAULT clause for a column in CREATE/ALTER TABLE statements. The ODBC driver and OLEDB provider for Access have diverged somewhat in their DDL support, so unfortunately we can get inconsistent results for the same DDL statement as illustrated by the following VBScript code using ADO:
OLEDB works fine ...
Option Explicit
Dim conn
Set conn = CreateObject("ADODB.Connection")
Dim connStr
connStr = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\Public\mdbTest.mdb"
conn.Open connStr
On Error Resume Next
conn.Execute "DROP TABLE Cheeses"
On Error GoTo 0
conn.Execute "CREATE TABLE Cheeses (Id LONG PRIMARY KEY, CheeseName TEXT(50) DEFAULT 'Cheddar')"
conn.Execute "INSERT INTO Cheeses (Id) VALUES (1)"
Dim rst
Set rst = CreateObject("ADODB.Recordset")
rst.Open "SELECT CheeseName FROM Cheeses WHERE Id = 1", conn
If rst("CheeseName").Value = "Cheddar" Then
WScript.Echo "Success"
End If
conn.Close
... but if we change the connection string to use ODBC ...
connStr = "Driver={Microsoft Access Driver (*.mdb, *.accdb)};Dbq=C:\Users\Public\mdbTest.mdb"
... then our attempt to execute the CREATE TABLE statement fails with
Microsoft OLE DB Provider for ODBC Drivers: [Microsoft][ODBC Microsoft Access Driver] Syntax error in CREATE TABLE statement.
TL;DR - You can't use the DEFAULT clause in a CREATE/ALTER TABLE statement under pyodbc.

Related

Getting 'bool object not callable' error when inserting data into sql server using pandas dataframe and pyodbc's fast_executemany()

I've lots of records to be inserted into sql server. I'm using pyodbc and cursor.fast_executemany() to achieve this. The regular cursor.execute() is inserting records too slowly.
I'm following this article as a reference: https://towardsdatascience.com/how-i-made-inserts-into-sql-server-100x-faster-with-pyodbc-5a0b5afdba5
sample code I'm using:
query = "SELECT * FROM dbo.my_table"
df = pd.read_sql(query, conn)
my_insert_statement = f"INSERT INTO myschema.new_table(colA, colB, colC, colD, colE) values(?,?,?,?,?)"
cursor = conn.cursor()
cursor.fast_executemany = True
cursor.fast_executemany(my_insert_statement,df.values.tolist())
conn.commit()
cursor.close()
conn.close()
But I keep getting the below error although I don't have any boolean columns.
'bool' object is not callable
I don't know how to surpass this error and I really need to insert records in bulk and quick into my database table.
Any ideas on why this error occurs and how to solve this?
The second line in the following snippet has to be wrong. You set fast_executemany to True and then try to call it with fast_executemany().
cursor.fast_executemany = True
cursor.fast_executemany(my_insert_statement,df.values.tolist())
I looked at your guide and you have to replace the second line in the snippet with:
cursor.executemany(my_insert_statement,df.values.tolist())

Error while connecting DB2/IDAA using ADFV2

I am trying to connect DB2/IDAA using ADFV2 - while executing simple query "select * from table" - I am getting below error:
Operation on target Copy data from IDAA failed: An error has occurred on the Source side. 'Type = Microsoft.HostIntegration.DrdaClient.DrdaException, Message = Exception or type' Microsoft.HostIntegration.Drda.Common.DrdaException 'was thrown. SQLSTATE = HY000 SQLCODE = -343, Source = Microsoft.HostIntegration.Drda.Requester, '
I checked a lot and tried various options but still it's an issue.
I tried query "select * from table with ur" - query to call with read-only but still get above result.
If I use query like select * from table; commit; - then activity succeeded but no record fetch.
Is anyone have solution ?
I have my linked service setup like this. additional connection properties value is : SET CURRENT QUERY ACCELERATION = ALL

ADODB Recordset: Operation not allowed when the object is closed [duplicate]

This question already has answers here:
Why can't I do a "with x as (...)" with ADODB and Oracle?
(2 answers)
Closed 4 years ago.
This a sample query producing the same error but I have much complex query.
sqlText = "with t as (select * from dual) select * from t"
Cmd.CommandText = sqlText
Set rs = Cmd.Execute
Do While Not rs.EOF
For col = 0 To rs.Fields.Count - 1
Str = Str & " - " & rs.Fields(col).Value
Next col
rs.MoveNext
Str = Str & vbNewLine
Loop
I don't get any error if I use the query without "with", example as
sqlText = "select * from dual"
That's why I wonder what difference it makes in excel VBA with the above two sql queries and like to know if there is workaround. I still can write the sql query differently to produce the same output without using "with" but that won't just make my doubt go away, so like to hear an explanation from experts.
Before the select statement, attach a declaration like this:
Dim sqlText As String
sqlText = "with t as (select * from dual) select * from t"
Hope that helps!
Your code should be like this:
Cmd.CommandText = sqlText
Set rs.Source = cmd
rs.Open
Do Until rs.EOF
Cmd.Execute is used for commands, e.g. INSERT or UPDATE. For queries you have to use Open
Which provide do you use? Most likely the Microsoft OLE DB Provider for Oracle (MSDAORA). This provider is deprecated for I think 20 years and should not be used. Certainly it does not support subquery factoring, i.e. WITH-Clause.

Load connecting tables from Cassandra in QlikView with DataSatx ODBC

I am new to both Cassandra (2.0) and QlikView (11).
I have two keyspaces (tables) with large amount of data in Cassandra and I want to load them to QlikView.
Since I can not load the entire set, filtering is necessary.
// In QlikView's edit script
ODBC CONNECT TO [DataStax Cassandra ODBC DSN64];
LOAD idsession,
logintime,
"h_id" as hid;
SQL SELECT *
FROM Cassandra.test.sessions
WHERE logintime > '2015-06-09'
ALLOW FILTERING;
LOAD idhost,
site;
SQL SELECT *
FROM Cassandra.test.hosts
WHERE idhost in hid;
The second query does not work, error from qlikview line 3:16 no viable alternative at input 'hid'.
My question: is it possible to get the h_ids from the first query and only collect the corresponding entities from the second table?
I assume that you can't do an Exists in the DataSyntax ODBC which may help. DataStax doc
This could be done with an external program like (C#) but I really want to do this in QlikView's script file:
// Not complete code
query = select * from sessions where loginTime > '2015-06-09';
foreach (var id in query) {
query2 = "select * from hosts where idhost = " + i;
}
EDIT
This can be solved when loading XML files:
TableA:
LOAD id,
itema
FROM
[C:\test1data.xlsx]
(ooxml, embedded labels);
TableB:
LOAD idb,
itemb,
ida
FROM
[C:\test2data.xlsx]
(ooxml, embedded labels) where(Exists (id,ida));
EDIT2
Besides the great answer from #i_saw_drones another solutions is to loop through ids.
For i = 1 to NoOfRows('Sessions')
Let cur_id = Peek('hid',i - 1,'Sessions');
LOAD
idhost,
site;
SQL SELECT *
FROM Cassandra.test.hosts
WHERE idhost = $(cur_id);
NEXT i
Nevertheless was the performance not the great. It took about 30 minutes to load around 300 K lines from Cassandra. The same queries were tested in a C# program with the connector and it took 9 sec. But that was just the query. Then you should write it to XML and then load it to QlikView.
The reason that the second query fails is because the WHERE clause is expecting to find a literal string list of values to look "in". For example:
LOAD
idhost,
site;
SQL SELECT *
FROM Cassandra.test.hosts
WHERE idhost in ('ID1', 'ID2', 'ID3', 'ID4');
The hid field returned by the first query is a QlikView list and as such cannot be immediately coerced into a string. We have to do a little more scripting to obtain a list of values from the first query in literal form, and then add that to the second query as part of the WHERE clause. The easiest way to do this is to concatenate all of your hids into a string and then use the string as part of your WHERE IN clause.
ODBC CONNECT TO [DataStax Cassandra ODBC DSN64];
MyData:
LOAD
idsession,
logintime,
"h_id" as hid;
SQL SELECT *
FROM Cassandra.test.sessions
WHERE logintime > '2015-06-09'
ALLOW FILTERING;
hid_entries:
LOAD
chr(39) & hids & chr(39) as hids;
LOAD
concat(hid, chr(39) & ',' & chr(39)) as hids;
LOAD DISTINCT
hid
RESIDENT MyData;
LET hid_values = '(' & peek('hids',0,'hid_entries') & ')';
DROP TABLE hid_entries;
LOAD
idhost,
site;
SQL SELECT *
FROM Cassandra.test.hosts
WHERE idhost in $(hid_values);

How to select All the Tables in Access DataBase 2007 using query

I am working in C#.net.I want to select all table names in the Access Database using Query. Can any one tell... How to select all the tables name from access database using query?.
Try like below it will help you...
string connectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\\myAccess2007file.accdb;Persist Security Info=False;";
OleDbConnection conn = new OleDbConnection(connectionString);
conn.Open();
DataTable tables = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables,new object[]{null,null,null,"TABLE"});
conn.Close();
Now the Datatable "tables" have all the access table Info..

Resources