I am trying to update a Sybase table via Microsofts ODBC API. The following is the basics of the C++ I am trying to execute. In table, TableNameXXX, ColumnNameXXX has a type of NVARCHAR( 200 ).
SQLWCHAR updateStatement[ 1024 ] = L"UPDATE TableNameXXX SET ColumnNameXXX = N 'Executive Chair эюя' WHERE PKEYXXX = 'VALUE'";
if( ret = SQLExecDirect( hstmt, ( SQLWCHAR* ) updateStatement, SQL_NTS ) != SQL_SUCCESS )
{
// Handle Error
}
The Sybase database has a CatalogCollation of 1252LATIN1, CharSet of windows-1252, Collation of 1252LATIN1, NcharCharSet of UTF-8 and an NcharCollation of UCA.
Once this works for the Sybase ODBC connection I need to get it to work in various other ODBC drivers for other databases.
The error i get is "[Sybase][ODBC Driver][SQL Anywhere]Syntax error near 'Executive Chair ' on line 1"
If i take out the Unicode characters and remove the N it will update.
Does anyone know how to get this to work? What am I missing?
I wrote a C# .net project using an ODBCConnection to a SQL Server database and am getting "sort of" the same error. I means sort of as this error contains the Unicode Text in the message whereas the Sybase ODBC error has "lost" the unicode.
static void Main(string[] args)
{
using (OdbcConnection odbc = new OdbcConnection("Dsn=UnicodeTest;UID=sa;PWD=password")) // ;stmt=SET NAMES 'utf8';CharSet=utf16"
//using (OdbcConnection odbc = new OdbcConnection("Dsn=Conversion;CharSet=utf8")) // ;stmt=SET NAMES 'utf8';CharSet=utf8
{
try
{
odbc.Open();
string queryString = "UPDATE TableNameXXX SET ColumnNameXXX = N 'Executive Chair эюя' WHERE PKEYXXX = 'AS000008'";
System.Console.Out.WriteLine(queryString);
OdbcCommand command = new OdbcCommand(queryString);
command.Connection = odbc;
int result = command.ExecuteNonQuery();
if( result == 1)
{
System.Diagnostics.Debug.WriteLine("Success");
}
}
catch(Exception ex)
{
System.Diagnostics.Debug.WriteLine(ex.StackTrace);
System.Diagnostics.Debug.WriteLine(ex.Message);
}
}
}
"ERROR [42000] [Microsoft][SQL Server Native Client 11.0][SQL Server]Incorrect syntax near 'Executive Chair эюя'."
There should be no space between the N and the Unicode enclosed string.
"UPDATE TableNameXXX SET ColumnNameXXX = N 'Executive Chair эюя' WHERE PKEYXXX = 'AS000008'"
should be
"UPDATE TableNameXXX SET ColumnNameXXX = N'Executive Chair эюя' WHERE PKEYXXX = 'AS000008'"
Related
I tried to add special symbols (i.e : æ_ø_å_Æ_Ø_Å_____£€$&#%¿) to Oracle database table VARCHAR2 column and different results noticed in following methods.
Database character set : SELECT * FROM nls_database_parameters WHERE parameter LIKE '%SET';
NLS_NCHAR_CHARACTERSET AL16UTF16
NLS_CHARACTERSET WE8MSWIN1252
SQL used :
INSERT INTO Test(C1) VALUES('æ_ø_å_Æ_Ø_Å_____£€$&#%¿');
Insert Directly via SQL Developer
Result : æ_ø_å_Æ_Ø_Å_____£€$&#%¿
Insert via SQLPlus
Result : ¿¿_¿¿_¿¿_¿¿_¿¿_¿¿_____¿¿¿¿¿$&#%¿¿
Insert via C++ code using SOCI library
Result :
While using database connection string (WINDOWS_1252 charset) : "oracle://service=<service> user=<user> password=<password> charset=178 ncharset=1000"; result was
æ_ø_å_Æ_Ø_Å_____£€$&#%¿
While using database connection string (UTF_8 charset) : "oracle://service=<service> user=<user> password=<password> charset=871ncharset=1000"; result was
æ_ø_å_Æ_Ø_Å_____£€$&#%¿
C++ code used :
std::string dbConnectionString = "oracle://service=<service> user=<user> password=<password> charset=178 ncharset=1000";
soci::session dbCon;
dbCon.open(dbConnectionString.c_str());
soci::statement *cursor = nullptr;
std::string selectString ="INSERT INTO Test(C1) VALUES('æ_ø_å_Æ_Ø_Å_____£€$&#%¿')";
try
{
cursor = new soci::statement(dbCon);
cursor ->alloc();
cursor ->prepare(selectString);
cursor ->define_and_bind();
cursor ->execute(true);
}
catch (soci::soci_error const & e)
{
std::cout <<"ERROR : ." << e.get_error_category() << " : "<<e.what()<< std::endl;
}
What is the reason for this inconsistent behavior?
I use sqlite on a c++ project, but I have a problem when i use WHERE on a column with TEXT values
I created a sqlite database:
CREATE TABLE User( id INTEGER PRIMARY KEY AUTOINCREMENT, name VARCHAR(24))
When i try to get the value of the column with VARCHAR values, it doesn't work, and return me a STATUS_CODE 101 just after the sqlite3_step :
int res = 0;
sqlite3_stmt *request;
char *sqlSelection = (char *)"SELECT * FROM User WHERE name='bob' ";
int id = 0;
res = sqlite3_prepare_v2(db, sqlSelection, strlen(sqlSelection), &request, NULL);
if (!res){
while (res == SQLITE_OK || res == SQLITE_ROW){
res = sqlite3_step(request);
if (res == SQLITE_OK || res == SQLITE_ROW ){
id = sqlite3_column_int(request, 0);
printf("User exist %i \n",id);
}
}
sqlite3_finalize(request);
I also tried with LIKE but it also doesn't work
SELECT * FROM User WHERE name LIKE '%bob%'
But when I execute the same code but for an INTERGER value
SELECT * FROM User WHERE id=1
It work fine.
In DB Browser for SQLite all requests work fine.
To solve the problem I searched what status code 101 means.
Here is what they said.
(101) SQLITE_DONE
The SQLITE_DONE result code indicates that an operation has completed.
The SQLITE_DONE result code is most commonly seen as a return value
from sqlite3_step() indicating that the SQL statement has run to
completion. But SQLITE_DONE can also be returned by other multi-step
interfaces such as sqlite3_backup_step().
https://sqlite.org/rescode.html
So, you're getting 101 because there is no more result from SELECT SQL.
The solution was to replace the VARCHAR fields by TEXT.
SQLite for c++ seems to don't manage VARCHAR fields when they are used after the WHERE
I have to read a column of VARCHAR2 type from Oracle table in my CPP code. When I tried to get the VARCHAR2 as a string using getString() of OCCI call, the code fails.
In the below code, I am concerned in getting table column COST_VALUE of VARCHAR2<50>
Code:
string sqlStmt = "SELECT \”REJECTED COST\”, APPROVED_COST, COST_VALUE FROM COST_TABLE where PART_NUM= 'PN4879-1'";
stmt = conn->createStatement(sqlStmt);
ResultSet *rset = stmt->executeQuery();
double dRejCost = 0;
double dAppCost = 0;
if(rset->next())
{
dRejCost = rset->getNumber(1);
dAppCost = rset->getNumber(2);
string strCost = rset->getString(3);
}
stmt->closeResultSet(rset);
conn->terminateStatement(stmt);
Error:
When I debugged I was able to see the value (from COST_TABLE column) being fetched in the string. But after the line (string strCost = rset->getString(3);) is executed, the application failed without any exception.
Please help with your expertise.
I am trying to run an SQL stored procedure through ADO in C++. The procedure is called (for argument's sake) testProcedure and expects two parameters: #param1 and #param2. Here is a trimmed version of the code in the execution method:
m_mCommandParameters[_T("param1")] = _T("foo");
m_mCommandParameters[_T("param2")] = _T("bar");
pCommand.CreateInstance(__uuidof(Command));
pCommand->ActiveConnection = link;
pCommand->CommandType = adCmdStoredProc;
pCommand->CommandText = _T("testProcedure");
pCommand->PutPrepared(true);
pCommand->NamedParameters = true;
// Set up the variant to store the parameter values
VARIANT vParamValue;
vParamValue.vt = VT_BSTR;
CString paramCount; // Stores the count for use as parameter name
// Iterate through set parameters and apply them to command
map<CString,CString>::iterator mItr;
for(mItr = m_mCommandParameters.begin(); mItr != m_mCommandParameters.end(); mItr++) {
paramCount = mItr->first;
vParamValue.bstrVal = _bstr_t(mItr->second);
// Append the parameter
if (mItr->second.IsEmpty()) {
_variant_t vtNULL;
vtNULL.vt = VT_NULL;
pCommand->Parameters->Append(
pCommand->CreateParameter(_bstr_t(L"#"+paramCount),adVarChar,adParamInput,10,vtNULL)
);
} else {
pCommand->Parameters->Append(
pCommand->CreateParameter(_bstr_t(L"#"+paramCount),adVarWChar,adParamInput,
//commandParameters[i].GetLength()+1,
sizeof(vParamValue),
_bstr_t(vParamValue))
);
}
}
_variant_t vRecordsAffected;
pRecordSet = pCommand->Execute(&vRecordsAffected,NULL,adCmdStoredProc);
My understanding is that this should essentially execute the following:
testProcedure #param1 = 'foo', #param2 = 'bar'
If I open SQL management studio and run the above it works fine. But when I try and run the C++ I get the error:
database error IDispatch error #3092 80040e14 query : testProcedure;
[Microsoft][ODBC SQL Server Driver]Syntax error or access violation.
I only have SQL express so don't have SQL profiler; I usually use Express Profiler but for some reason it doesn't display any trace on stored procedured. So I am not sure how to start debugging this.
Thanks!
I want to create a stored procedure with 4 insert and i have this code
Driver * driver = get_driver_instance();
auto_ptr< Connection > vCon(driver->connect(getHost(), getUser(), getPassword()));
vCon->setSchema(getDB());
auto_ptr< Statement > vStmt(vCon->createStatement());
vStmt->execute("DROP PROCEDURE IF EXISTS add");
vStmt->execute("CREATE PROCEDURE add() begin DECLARE vEvId int DEFAULT 0; DECLARE vAdrEvId int DEFAULT 0; insert into adrEv(den) values('test'); select last_insert_id() into vAdrEvId; insert into ev(den,adrEvId) values('test',vAdrEvId); select last_insert_id() into vEvId; ... other insert ... END;");
vStmt->execute("CALL add()");
vStmt->execute("DROP PROCEDURE IF EXISTS add");
whay i get this error # ERR: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'add' at line 1 (MySQL error code: 1064, SQLState: 42000 )
ADD is a reserved word for MySQL. Try using a more specific/descriptive procedure name.