1 交叉编译工具的安装 cd/work/tools arm-linux-gcc -v(完成)
2 U-Boot移植
3 测试
#include <stdio.h>
#include <stdlib.h>
#include <sqlite3.h>
int main(void)
{
sqlite3 *db=NULL;
char *zErrMsg = 0;
int rc;
int i=0;
rc = sqlite3_open("my.db" , &db);
if( rc )
{
fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db));
sqlite3_close(db);
return 0;
}
char *sql = " create table student(id , name , sex , age)";
//使用sql字符串指定的sql语言 创建数据表SensorData
sqlite3_exec( db , sql , 0 ,0 , &zErrMsg );
//插入数据到数据表
sql = "insert into student values(1, 'jack', 'M', 20);" ;
sqlite3_exec( db , sql , 0 , 0 , &zErrMsg );
//插入数据到数据表
sql = "insert into student values(1, 'tom', 'M', 20);" ;
sqlite3_exec( db , sql , 0 , 0 , &zErrMsg );
sql = "insert into student values(1, 'mary', 'W', 10);" ;
sqlite3_exec( db , sql , 0 , 0 , &zErrMsg );
sql = "select * from student ";
sqlite3_exec( db , sql , 0 , 0 , &zErrMsg );
int nrow = 0, ncolumn = 0;
char **azResult;
//从数据表查询数据
sql = "select * from student ";
sqlite3_get_table( db , sql , &azResult , &nrow , &ncolumn , &zErrMsg );
printf( "row:%d column=%d" , nrow , ncolumn );
printf( "The result of querying is :" );
for( i=0 ; i<( nrow + 1 ) * ncolumn ; i++ )
{
printf( "azResult[%d] = %s \r\n", i , azResult[i] );
}
sqlite3_free_table( azResult );
sql = "update student set age = 24 where age = 20";
printf("\n");
sqlite3_exec( db , sql , 0 , 0 , &zErrMsg );
nrow = 0;
ncolumn = 0;
char **secondResult;
sql = "select * from student ";
sqlite3_get_table( db , sql , &secondResult , &nrow , &ncolumn , &zErrMsg );
printf( "row:%d column=%d" , nrow , ncolumn );
printf( "The result of querying is : \n" );
for( i=0 ; i<( nrow + 1 ) * ncolumn ; i++ )
{
printf( "secondResult[%d] = %s \r\n", i , secondResult[i] );
}
sqlite3_free_table( secondResult );
printf("\n");
sqlite3_close(db);
return 0;
}
//////////////////////////////////////////////////////////////////////////////////////////////////
菜单程序
#include<stdio.h>
#include<stdlib.h>
#include<sqlite3.h>
#include<string.h>
#define BUFFSIZE 256
char *zerrmsg=0;
char *errmsg;
char strDB[BUFFSIZE];
void creat_table(sqlite3 *db)
{
int ret;
//create the table
sprintf(strDB,"CREATE TABLE STUDENT(\
NUMBER INTEGER PRIMARY KEY ,\
NAME VARCHAR(12),\
AGE INTEGER,\
SCORE INTEGER\
);");
ret = sqlite3_exec(db, strDB ,NULL , NULL, &zerrmsg);
if(ret!=0)
{
printf("create the table failed!\n");
exit(-1);
}
printf("create the table success!\n");
}
void insert_data(sqlite3 *db)
{
int number,age,score;
char name[20];
printf("输入学号: ");
scanf("%d",&number);
getchar();
printf("输入名字: ");
scanf("%s",name);
getchar();
printf("输入年龄: ");
scanf("%d",&age);
getchar();
printf("输入成绩 ");
scanf("%d",&score);
getchar();
char *sql=sqlite3_mprintf("insert into student values('%d','%s','%d','%d')",number,name,age,score);
if(sqlite3_exec(db,sql,NULL,NULL,&errmsg) != SQLITE_OK)
{
perror("sqlite3_exec");
exit(-1);
}
else
printf("insert success!!\n");
return;
}
void delete_data(sqlite3 *db)
{
int num;
printf("please input the number you want to delete\n");
scanf("%d",&num);
getchar();
char *sql=sqlite3_mprintf("delete from student where number ='%d'",num);
if(sqlite3_exec(db,sql,NULL,NULL,&errmsg) != SQLITE_OK)
{
perror("sqlite3_exec_delete");
exit(-1);
}
else
printf("delete success!!\n");
return;
}
void updata_data(sqlite3 * db)
{
int num,age,score;
char name[20];
printf("please input the number you want to updata\n");
scanf("%d",&num);
getchar();
printf("输入名字: ");
scanf("%s",name);
getchar();
printf("输入年龄: ");
scanf("%d",&age);
getchar();
printf("输入成绩:");
scanf("%d",&score);
getchar();
// char sql[1024];
// sprintf(sql,"update student set name='%s',age='%d',score='%d' where number='%d'",name,age,score,num);
char *sql=sqlite3_mprintf("update student set name='%s',age='%d',score='%d' where number='%d'",name,age,score,num);
if(sqlite3_exec(db,sql,NULL,NULL,&errmsg) != SQLITE_OK)
{
perror("sqlite3_exec_update");
exit(-1);
}
else
printf("update success!!\n");
return;
}
void show_data(sqlite3 *db)
{
char ** resultp;
int nrow,ncolumn,i,j,index;
char *sql="select * from student";
if(sqlite3_get_table(db,sql,&resultp,&nrow,&ncolumn,&errmsg) != SQLITE_OK)
{
perror("sqlite3_get_table");
exit(-1);
}
index = ncolumn;
for(i = 0 ;i< ncolumn; i++)
printf("%s\t",resultp[i]);
printf("\n");
for(i = 0;i< nrow;i++)
{
for(j = 0;j< ncolumn;j++)
printf("%s\t",resultp[index++]);
printf("\n");
}
return ;
}
void quit(sqlite3 *db)
{
printf("BYBYE!!\n");
sqlite3_close(db);
exit(0);
}
int main()
{
int nu;
sqlite3 *db;
if(sqlite3_open("student.db",&db) != SQLITE_OK)
{
perror("sqlite3_open");
exit(-1);
}
while(1)
{
printf("************\n");
printf("*1.输入数据*\n");
printf("*2.删除数据*\n");
printf("*3.更新数据*\n");
printf("*4.显示数据*\n");
printf("*5.退出 *\n");
printf("************\n");
printf("请输入你要选择的数:\n");
scanf("%d",&nu);
getchar();
switch(nu)
{
case 0:
creat_table(db);
break;
case 1:
insert_data(db);
break;
case 2:
delete_data(db);
break;
case 3:
updata_data(db);
break;
case 4:
show_data(db);
break;
case 5 :
quit(db);
break;
}
}
sqlite3_close(db); //关闭数据库
return 0;
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
create table student (number, name, age, score);
insert into student values (1, 'Jack', 'M',20 );
insert into student values (2, 'Tom', 'M',24 );
insert into student values (3, 'mary', 'W',28 );
select * from student;
if(sqlite3_exec(db,sql,NULL,NULL,&errmsg) != SQLITE_OK)
if(sqlite3_exec(db,sql,NULL,NULL,&errmsg) != SQLITE_OK)
gcc -o a -I/usr/local/sqlite_x86/include -L/usr/local/sqlite_x86/lib a.c -lsqlite3 -static -lpthread
移植到ARM
./configure --prefix=/usr/local/sqlite_arm --disable-tcl --host=arm-linux
./configure --prefix=/usr/local/sqlite_arm --disable-tcl --host=arm-none-linux-gnueabi CC=arm-none-linux-gnueabi-gcc
make
make install
arm-linux-gcc -o caidan -I/usr/local/sqlite_x86/include -L/usr/local/sqlite_x86/lib caidan.c -lsqlite3 -static -lpthread
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sqlite3.h>
#define BUFFSIZE 256
#define OPTIONSIZE 20
char *zErrMsg = 0;
char strDB[BUFFSIZE];
static void
tableCreat(sqlite3 *db)
{
int ret;
//create the table
sprintf(strDB,"CREATE TABLE STUSCORE(\
ID INTEGER PRIMARY KEY ,\
STUID INTEGER,\
NAME VARCHAR(12),\
SCORE INTEGER\
);");
ret = sqlite3_exec(db, strDB ,0 , 0, &zErrMsg);
if(ret!= 0)
{
printf("create the ta
评论0