Click here to Skip to main content
15,891,762 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to add the value of my CString into the datbase i already created.

C++
CString textvalue  = _T("DarkKnight");
const char *pSQL[1];
pSQL[0] = "insert into Movies (Title, Director, Year) values (''+textvalue+'', 'Nolan', 2008)";
int rc = sqlite3_exec(database, pSQL[0], callback, 0, &zErrMsg);


When i am executing the command i am getting an error message "There is no column by name textvalue in the database".

How do i insert the value "DarkKnight" into the SQLite database at runtime.
Posted
Comments
[no name] 21-Jun-12 16:34pm    
pSQL[0] = "insert into Movies (Title, Director, Year) values ('" + textvalue + "', 'Nolan', 2008)";

1 solution

Here's the code I use in a C++ cgi module.

C++
sprintf(buffer, "INSERT INTO `cds` (title,artist,year) VALUES('%s','%s',%d)", bTitle.c_str(), bArtist.c_str(), yr);
queryStr = buffer;
db.exe(queryStr);


At a guess, it looks like you've used mySql before in PHP from the way you expect to have the contents of the variable substituted for the variable name.


EDIT: something I whacked together in MFC:
C++
void CmfcSqliteDlg::OnBnClickedButton1()
{
	USES_CONVERSION;
	CString queryStr;
	CString artStr, albStr, yrStr;
	
	artStr = _T("DeadMau5");
	albStr = _T("4x4 = 12");
	yrStr = _T("2010");

	queryStr.Format(L"insert into cds (title, artist, year) values('%s', '%s', %s)", artStr, albStr, yrStr);
	char *charStr = T2A(queryStr);

	MessageBox(queryStr, _T("Query to execute"));
	MessageBoxA(NULL, charStr, "Query to exeute", MB_OK);
}
 
Share this answer
 
v2
Comments
Sandeep Mewara 22-Jun-12 1:53am    
My 5!
amarasat 22-Jun-12 10:29am    
Thanks alot for your help!! Both ways have worked!!

My 5 too!
amarasat 22-Jun-12 15:59pm    
How do i solve this:

CString textvalue = _T("120");
CString Tag = _T("Plugin1@PLG.Value1@Value");
queryStr.Format(L"update Table set Value=%s where Tag=%s", textvalue, Tag);
char *pSQL[0] = T2A(queryStr);
int rc = sqlite3_exec(database, pSQL[i], callback, 0, &zErrMsg);

gives me an error: "near "@PLG": syntax error"

also for Tag = _T("Main Plugin1@PLG.Value1@Value");

gives me an error: "near "Plugin1": syntax error"

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900