string conversion to COleDateTime (CRecordset) - c++

In a CRecordset, i want to store two dates into a database table. The fields on the database are from type datetime (MS SQL).
There are two variables named validfrom and validto from type string. The dateformat from the string-variables are DD.MM.YYYY. m_Validfrom and m_Validto is from type COleDateTime.
At CFSC_FULLTRUCK::Update(), i get a conversion error.
"Invalid character value for cast specification"
void CFSC_FULLTRUCK::setfulltruck(int nr ,CString ort,CString country ,CString zone, double cost ,CString file_index, string validfrom, string validto, double fuelinpercent, CString remark)
{
CFSC_FULLTRUCK::Open();
CFSC_FULLTRUCK::AddNew();
m_NR = nr;
m_ORT =ort;
m_COUNTRY =country;
m_ZONENO = zone;
m_COST = cost;
m_FILE_INDEX = file_index;
if(validfrom == ""){validfrom = "01.01.2001";}
if(validto == ""){validto = "31.12.2020";}
m_Validfrom.ParseDateTime(validfrom);
m_Validto.ParseDateTime(validto);
m_Fuelinpercent = fuelinpercent;
m_Remark = remark;
CFSC_FULLTRUCK::Update();
CFSC_FULLTRUCK::Close();
}

Related

How to insert a record into Microsoft Access using MFC?

How can I insert record in Microsoft Access?
CString SqlString;
CString name="I want to add this variable in Table3";
SqlString = "INSERT INTO Table3 (Name,Numbers) VALUES (name,099)";
When I do it that way gives the following error:
Database error:Too few parameters.Expected 1.
This is a snippet from my own application:
BOOL CCommunityTalksApp::SetRecordForTalkNumber(int iTalkNumber, UINT uID, CString &rStrError)
{
CDatabase *pDatabase;
CString strSQL, strField;
BOOL bOK;
pDatabase = theApp.GetDatabase();
if(pDatabase != nullptr)
{
if (iTalkNumber == 9999)
strField = _T(" ");
else
strField.LoadString(uID);
strSQL.Format(_T("INSERT INTO [Public Talk Titles] ([Theme], [Category], [Talk Number]) VALUES ('%s', 'NS', %d)"), strField, iTalkNumber);
TRY
{
pDatabase->ExecuteSQL((LPCTSTR)strSQL);
bOK = TRUE;
}
CATCH(CDBException, Except)
{
rStrError = Except->m_strError;
bOK = FALSE;
}
END_CATCH
}
return bOK;
}
As you can see:
Use [ and ] to wrap the table and field names to address any issues with spaces.
Qualify the field names first — particularly if you are only populating certain field values.
Wrap the string values with single quotes.
So:
SqlString = "INSERT INTO Table3 (Name,Numbers) VALUES (name,099)";
Would be something like:
SqlString = "INSERT INTO [Table3] ([Name],[Numbers]) VALUES ('name',099)";
I appreciate that the square brackets are not needed for your table / field names though.

Using Univocity, how can I convert a date string value to a Date type in Java

I'll like to parse column zero in a csv file to a particular datatype, in this example a Date Object.
The method below is what I use currently to parse a csv file but I don't know how to incorporate this requirement.
import java.sql.Date;
public class Data {
#Parsed(index = 0)
private Date date;
}
}
public <T> List<T> convertFileToData(File file, Class<T> clazz) {
BeanListProcessor<T> rowProcessor = new BeanListProcessor<>(clazz);
CsvParserSettings settings = new CsvParserSettings();
settings.setProcessor(rowProcessor);
settings.setHeaderExtractionEnabled(true);
CsvParser parser = new CsvParser(settings);
parser.parseAll(file);
return rowProcessor.getBeans();
}
All you need is to define the format(s) of your date and you are set:
#Format(formats = {"dd-MMM-yyyy", "yyyy-MM-dd"})
#Parsed(index = 0)
private Date date;
}
As an extra suggestion, you can also replace a lot of your code by using the CsvRoutines class. Try this:
List<T> beanList = new CsvRoutines(settings).parseAll(clazz, file);
Hope it helps.

SQLite Update table

Trying to update table by user specified values. But the values are not getting updated.
cout<<"\nEnter Ac No"<<endl;
cin>>ac;
cout<<"\nEnter Amount"<<endl;
cin>>amt;
/* Create merged SQL statement */
sql = "UPDATE RECORDS set BAL = '%d' where ACCOUNT_NO = '%d'",amt, ac;
/* Execute SQL statement */
rc = sqlite3_exec(db, sql, callback, (void*)data, &zErrMsg);
If I replace BAL and ACCOUNT_NO by some integer value instead of place holder then it is working fine.
Your sql string is not being created properly.
If you expect this code
sql = "UPDATE RECORDS set BAL = '%d' where ACCOUNT_NO = '%d'",amt, ac;
to result in
"UPDATE RECORDS set BAL = '1' where ACCOUNT_NO = '2'"
where
amt= 1 and ac = 2 then you need to use a string formatting call like this.
// the buffer where your sql statement will live
char sql[1024];
// write the SQL statment with values into the buffer
_snprintf(sql,sizeof(sql)-1, "UPDATE RECORDS set BAL = '%d' where ACCOUNT_NO = '%d'",amt, ac);
buff[sizeof(sql)-1]='\0';
On your particular platform _snprintf(...) might be snprintf(..) or another similarly named function. Also your compiler may warn about buffer manipulation security vulnerabilities. Choose the appropriate substitute for your needs

CreateQuery constructor with getting parameter list

I'm new here and this is my second question, but as the first, I have a problem already tried to solve in different ways, but have not found a solution, so I'm resorting to you, here we go.
My code:
string sql = "SELECT NEW Filial(fil.NumSequencial, fil.Numero, fil.Nome, fil.Cnpj, fil.Empresa, fil.LstUsuario)" +
" FROM Filial fil" +
" join fetch fil.LstUsuario usrFil";
IQuery query = session.CreateQuery(sql);
lstFilial = (List<Filial>)query.List<Filial>();
I've tried with and without the line "join fetch", but the error is always the same:
{"illegal syntax near collection: id [SELECT NEW Filial(fil.NumSequencial, fil.Numero, fil.Nome, fil.Cnpj, fil.Empresa, fil.LstUsuario) FROM Leitor_NFe_XML_Entidade.Filial fil\tjoin fetch fil.LstUsuario usrFil]"}
The constructor:
public Filial(long numSequencial, int numero, string nome, string cnpj, Empresa empresa, IList<Usuario> lstUsuario)
{
this.numSequencial = numSequencial;
this.numero = numero;
this.nome = nome;
this.cnpj = cnpj;
this.empresa = empresa;
this.lstUsuario = lstUsuario;
}
Am thankful since already

Invalid Argument to getUInt64 when retrieving LAST_INSERT_ID()

I have added a record to my table which auto-increments the primary key. I am having no luck retrieving this new value. The MySQL documents say to use the SELECT LAST_INSERT_ID(); in a query. I have done this, but can't retrieve the results.
According the the metadata of the result set, the data type is BIGINT and the column name is LAST_INSERT_ID(). The C++ connector has a getUInt64() for the result set, which I assume is the correct method to use.
The ResultSet class declaration contains the following:
virtual uint64_t getUInt64(uint32_t columnIndex) const = 0;
virtual uint64_t getUInt64(const std::string& columnLabel) const = 0;
The documentation does not state whether the columnIndex is zero based or one based. I tried both and get sql::InvalidArgumentException for both cases.
Using the result set metadata, I retrieved the column name and passed it directly to the getUInt64 method and still receive the sql::InvalidArgumentException. This not a good indication (when the returned column name doesn't work when fetching the data).
Here is my code fragment:
std::string query_text;
query_text = "SELECT LAST_INSERT_ID();";
boost::shared_ptr<sql::Statement> query(m_db_connection->createStatement());
boost::shared_ptr<sql::ResultSet> query_results(query->executeQuery(query_text));
long id_value = 0;
if (query_results)
{
ResultSetMetaData p_metadata = NULL;
p_metadata = query_results->getMetaData();
unsigned int columns = 0;
columns = p_metadata->getColumnCount();
std::string column_label;
std::string column_name;
std::string column_type;
for (i = 0; i < columns; ++i)
{
column_label = p_metadata->getColumnLabel(i);
column_name = p_metadata->getColumnName(i);
column_type = p_metadata->getColumnTypeName(i);
wxLogDebug("Column label: \"%s\"\nColumn name: \"%s\"\nColumn type: \"%s\"\n",
column_label.c_str(),
column_name.c_str(),
column_type.c_str());
}
unsigned int column_index = 0;
column_index = query_results->findColumn(column_name);
// The value of column_index is 1 (one).
// All of the following will generate sql::InvalidArgumentException
id_value = query_results->getUInt64(column_index);
id_value = query_results->getUInt64(column_name);
id_value = query_results->getUInt64(0);
id_value = query_results->getUInt64(1);
id_record.set_record_id(id_value);
}
Here is the debug output (from wxLogDebug):
10:50:58: Column label: "LAST_INSERT_ID()"
Column name: "LAST_INSERT_ID()"
Column type: "BIGINT"
My Question: How do I retrieve the LAST_INSERT_ID() using the MySQL C++ Connector?
Do I need to use a prepared statement instead?
I am using MySQL Connector C++ 1.0.5 on Windows Vista and Windows XP with Visual Studio 9 (2008).
Resolved.
I inserted a query_results->next() before retrieving the data and that worked.