Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C++ Sqlite
I want to add the value of my CString into the datbase i already created.
 
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 21-Jun-12 11:32am
amarasat1.3K
Comments
Wes Aday at 21-Jun-12 16:34pm
   
pSQL[0] = "insert into Movies (Title, Director, Year) values ('" + textvalue + "', 'Nolan', 2008)";

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Here's the code I use in a C++ cgi module.
 
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:
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);
}
  Permalink  
v2
Comments
Sandeep Mewara at 22-Jun-12 1:53am
   
My 5!
amarasat at 22-Jun-12 10:29am
   
Thanks alot for your help!! Both ways have worked!!
 
My 5 too!
amarasat at 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)

  Print Answers RSS
0 DamithSL 265
1 CPallini 235
2 OriginalGriff 233
3 Maciej Los 175
4 George Jonsson 170
0 OriginalGriff 5,305
1 DamithSL 4,382
2 Maciej Los 3,760
3 Kornfeld Eliyahu Peter 3,470
4 Sergey Alexandrovich Kryukov 2,901


Advertise | Privacy | Mobile
Web03 | 2.8.141216.1 | Last Updated 22 Jun 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100