以下是我的代码的外观
lk_dispclr();
strcpy(menu.title,"Login");
int res=0;
prn_open();
lk_bkl_timeout(20);
lk_dispclr();
lcd::DisplayText(1,0,"1.User Menu ",0);
lcd::DisplayText(4,0,"Press any key",0);
lk_getkey();
while(1)
{
char user[10]={0};
lk_dispclr();
lcd::DisplayText(2,0,"Enter Username",0);
res=lk_getalpha(4,0,(unsigned char *)user,9,strlen(user),0);
if(res>0)
{
user[res]='\0';
printf("Username is %s %d\n",user,res);
klok::pc::User userObj;
if(klok::pc::User::FromDatabase(getDatabase(),user,userObj) != 0)
{
printf("No Such User %s\n", user);
continue;
}
if(userObj.id == user)
{
while(1)
{
char pwd[10]={0};
lk_dispclr();
lcd::DisplayText(2,0,"Enter Password",0);
res=lk_getpassword((unsigned char *)pwd,4,9);
if (res>0)
{
pwd[res]='\0';
printf("Password is %s %d\n",pwd,res);
if(userObj.password == pwd )
{
gUserName = user;
main_menu(user,pwd);
printf("main_menu\n");
}
else
{
goto AGAIN_ASK_USER_DETAILS;
}
}
}
}
}
AGAIN_ASK_USER_DETAILS:
while(false);
}
数据库助手
#include "PosDataStructures.hpp"
#include <SQLiteCpp/SQLiteCpp.h>
namespace klok {
namespace pc {
namespace {
using SQLite::Database;
using SQLite::Column;
using SQLite::Statement;
}
int32_t User::FromDatabase(SQLite::Database & db,const char * id,User & outUser){
const std::string queryString = "SELECT * FROM ? WHERE User_ID=?";
try
{
SQLite::Statement query(db,User::Queries::SELECT_USER_WITH_ID_FROM_TABLE);
query.bind(1,User::Queries::TABLE_NAME);
query.bind(2,id);
if(query.executeStep())
{
outUser.id = query.getColumn(0).getString();
outUser.name = query.getColumn(1).getString();
outUser.password = query.getColumn(2).getString();
return 0;
}
else
{
return -1;
}
}
catch(std::exception & e)
{
std::printf("User::FromDatabase -> Fatal Error %s\n", e.what());
return -1;
}
return 0;
}
int32_t User::CreateTable(SQLite::Database & db,bool dropIfExist){
const std::string queryString = "SELECT * FROM pay_coll_user WHERE User_ID=?";
try
{
SQLite::Statement query(db,queryString);
if(query.executeStep()) {
return 0;
}
else {
return -1;
}
}
catch(std::exception & e)
{
std::printf("User::CreateTable -> Fatal Error %s\n", e.what());
return -1;
}
return 0;
}
const char * User::Queries::TABLE_NAME = "pay_coll_user";
const char * User::Queries::SELECT_USER_WITH_ID_FROM_TABLE = "SELECT * FROM ? WHERE User_ID=?";
const char * User::Queries::CREATE_USER_TABLE_QUERY = "CREATE TABLE ? ("
"User_ID INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,"
"User_Name VARCHAR(20) NOT NULL UNIQUE,"
"Password VARCHAR(8) NOT NULL );";
const char * User::Queries::DROP_USER_TABLE_QUERY = "DROP TABLE ? IF EXISTS;";
}
}
在我的数据库( PayCollect.db )中我有表pay_coll_user,其中包含User_ID,用户名,密码列
第一个用户ID是101010,名字保罗,通过123456
但是当我在设备上测试此代码时会抛出错误
User :: FromDatabase - &gt; “?”附近的致命错误:语法错误 没有这样的用户101010