I recently switched form using a Postgres driver to the Amazon Redshift ODBC driver to access my Redshift datastore.
I have one C# process that started throwing errors and I narrowed it down to the following:
var connection = new OdbcConnection(connectionString);
connection.Open();
var command = new OdbcCommand("update test set deleted = ? where status = ?", connection);
command.Parameters.AddWithValue("#deleted", null);
command.Parameters.AddWithValue("#status", "to_delete");
var result = command.ExecuteNonQuery();
connection.Close();
The error I am getting is "ERROR [HY000] [Amazon][Amazon Redshift] (30) Error occurred while trying to execute a query: [SQLState HY000] Default parameter is not supported."
The same code worked with previous driver. It seems the Redshift driver does not allow setting nulls using "= null".
How can I set a db field value to null using this driver?
Related
Trying to switch database using Database (database name); query and then execute a query on that database in two separate queries.
However, I got this error: The cursor has been previously released and is unavailable. What does this mean?
Here is the code:
using (OdbcCommand command = new OdbcCommand(null, odbcConnection))
{
if (switchDBName != null)
{
command.CommandText = "Database " + switchDBName + ";";
command.ExecuteNonQuery();
Console.WriteLine("Switched to database: " + switchDBName + " Successfully.");
}
command.CommandTimeout = 0;
command.CommandText = query;
using (OdbcDataReader datareader = command.ExecuteReader())
{
DataTable resultDT = new DataTable();
resultDT.Load(datareader);
Console.WriteLine("ExecuteODBCQuery -- Finished. There are " + resultDT.Rows.Count + " rows.");
return resultDT;
}
}
If I'm following the logic correctly:
you have two different database connections;
you create a cursor-based statement using one connection;
you switch to the other database connection;
you get an error indicating that the cursor is not available on the current connection
albeit with not wholly appropriate wording in the error message.
A prepared statement or a cursor-based statement is associated with a connection. The same statement will not work for another connection; you have to recreate the cursor or prepared statement for each database connection.
You must know which connection was used for each prepared statement or cursor-based statement. You can switch connections between uses, but when you try to use a statement, the connection must be the same as was in use when it was prepared.
I try to run some basic code to test Sqlite in y project and the project always stops/exits on the call to Open().
Then in the debug window, I see that "Activation of the Windows Store app 'f398a499-0092-462e-9b50-aab3651b4b69_kxamnsbhqvv2e!App' failed with error 'Windows was unable to communicate with the target application. This usually indicates that the target application's process aborted. More information may be available in the Debug pane of the Output window (Debug->Windows->Output)'."
Version of Microsoft.Data.Sqlite is 1.0.0
Version of Microsoft.NETCore.UniversalWindowsPlatform is 6.0.6
Here is my code :
SqliteEngine.UseWinSqlite3(); //Configuring library to use SDK version of SQLite
using (SqliteConnection db = new SqliteConnection("Filename=sqliteSample.db"))
{
db.Open();
var tableCommand = "CREATE TABLE IF NOT EXISTS MyTable (Primary_Key INTEGER PRIMARY KEY AUTOINCREMENT, Text_Entry NVARCHAR(2048) NULL)";
var createTable = new SqliteCommand(tableCommand, db);
try
{
createTable.ExecuteReader();
}
catch (SqliteException e)
{
//Do nothing
}
}
Thanks
Changed the version of Sqlite to 1.1.1 and now it works.
I created a SQLite database via SQLiteManager. I have a table called Envelopes, and other tables. When I access it from code (below) I get an exception saying that the table I'm trying to access doesn't exist. I've tried both Envelopes and ZVDB.Envelopes, and yes I've double checked my spelling of table names.
var conn = new SqliteConnection("Data Source=ZVDB");
using ( var cmd = conn.CreateCommand()) {
conn.Open();
cmd.CommandType = CommandType.Text;
cmd.CommandText = "insert into Envelopes (Name, Remainder, Budget) values (#1, #2, #3); select ##identity";
cmd.Parameters.Add("#1", DbType.String).Value = env.Name;
cmd.Parameters.Add("#2", DbType.Int32).Value = env.Remainder;
cmd.Parameters.Add("#3", DbType.Int32).Value = env.Budget;
env.ID = (int) cmd.ExecuteScalar();
}
I have it in my MonoDevelop project, set with the proper action.
This means the path to the database is wrong.
In SQLite when you open a database that doesn't exist, by default it will just create a blank new one. No error.
When you then try to read or write to a table that doesn't exist, you get the error you described.
In the connection string you specified simply ZVDB with no path. Try specifying a full absolute path.
Samuel's answer is correct - if you want to access the database you need to ensure that you've included the database in your solution, set the 'build action' to "Content" and set 'Copt to output directory' to "Always copy".
I would like to run an SQL query on an iSeries (...or "System i" or "AS/400"...) machine as part of a Nagios check, but haven't found a suitable way of interfacing the database yet.
IBM suggests using the ODBC driver of System i Access for Linux with unixODBC, but since both systems are new to me, I'd like to know if there are other ways of doing this.
Hacks involving telnet and expect are perfectly fine. :-)
I think this would be the simplest ...
Same as Access for Linux but it is the open source version: JTOpen
example taken from iSeries Information Center:
// Connect to the server.
Connection c = DriverManager.getConnection("jdbc:as400://mySystem");
// Create a Statement object.
Statement s = c.createStatement();
// Run an SQL statement that creates
// a table in the database.
s.executeUpdate("CREATE TABLE MYLIBRARY.MYTABLE (NAME VARCHAR(20), ID INTEGER)");
// Run an SQL statement that inserts
// a record into the table.
s.executeUpdate("INSERT INTO MYLIBRARY.MYTABLE (NAME, ID) VALUES ('DAVE', 123)");
// Run an SQL statement that inserts
// a record into the table.
s.executeUpdate("INSERT INTO MYLIBRARY.MYTABLE (NAME, ID) VALUES ('CINDY', 456)");
// Run an SQL query on the table.
ResultSet rs = s.executeQuery("SELECT * FROM MYLIBRARY.MYTABLE");
// Close the Statement and the
// Connection.
s.close();
c.close();
Found at Nagios Exchange:
DB2 Checker
Download DB2 Checker
untested...
I had this source on my disk. It's a good example. For clarity, I did change the machine, catalog and table name in the source. Hope it helps. It uses the JDBC driver from the JTOpen project.
Notice that with this specific driver you can access the iSeries DB2 database as any other database. Also, the DB2 database on the iSeries is just one of the versions of the IBM Universal Database. However, you can do iSeries specific tricks, even with the JDBC driver. But if you want to stay SQL defaults only, that is fine too.
import java.sql.*;
public class TestSQL {
public static void main(String[] args) {
try {
DriverManager
.registerDriver(new com.ibm.as400.access.AS400JDBCDriver());
Connection conn = DriverManager
.getConnection("jdbc:as400://YOURISERIES/YOURLIBRARY");
Statement select = conn.createStatement();
ResultSet rs = select.executeQuery("select * from whatever");
ResultSetMetaData rsmd = rs.getMetaData();
while (rs.next()) {
for (int x=1; x <= rsmd.getColumnCount(); x++) {
System.out.print(rs.getString(x));
}
System.out.println();
}
} catch (Exception e) {
System.out.println("error: " + e.getMessage());
}
System.exit(0);
}
}
Hi I've got this code which uses Access to open/store data in database. I need to convert this to use SQLite. However I'm finding it difficult to find the appropriate connection string. How should I go about doing this? Thanks
var dbaseConnection = Server.CreateObject("ADODB.Connection");
var pathToDbase = Server.MapPath("contacts.mdb");
var connectionString = "Provider=Microsoft.ACE.OLEDB.12.0; Data Source=" + pathToDbase;
dbaseConnection.Open(connectionString);
This is just test code but I am trying to just read and display information from an sqlite database.
<table>
<tr><th>Name</th><th>Surname</th></tr>
<%
var dbaseConnection = Server.CreateObject("ADODB.Connection");
var connectionString = "DRIVER=SQLite3 ODBC Driver; Database= path to test.db; LongNames=0; Timeout=1000; NoTXN=0; SyncPragma=NORMAL; StepAPI=0;";
dbaseConnection.Open(connectionString);
var query = "Select * from test order by name asc";
var recordSet = dbaseConnection.Execute(query);
while (!recordSet.Eof) {
Response.write("<tr><td>" + recordSet("name") + '</td><td>' + recordSet("surname") + "</td></tr>");
recordSet.moveNext();
}
recordSet.Close();
dbaseConnection.Close();
%>
</table>
</body>
I am now getting this error
Microsoft OLE DB Provider for ODBC Drivers error '80004005'
connect failed.
Any thoughts? I've activated 32 bit apps in IIS.
DRIVER=SQLite3 ODBC
Driver;Database=c:\mydb.db;LongNames=0;Timeout=1000;NoTXN=0;
SyncPragma=NORMAL;StepAPI=0;
Reference:
https://www.connectionstrings.com/sqlite/