我的桌子是
CREATE TABLE IF NOT EXISTS STUDENTS "
"("
"ID INTEGER PRIMARY KEY AUTOINCREMENT, "
"NAME TEXT NOT NULL, "
"SURNAME TEXT, "
"STREETNO INTEGER, "
"STREETNAME TEXT, "
"SUBURB TEXT NOT NULL, "
"POSTCODE INTEGER NOT NULL, "
"STUDENTNO INTEGER NOT NULL, "
"DOB TEXT NOT NULL, "
"GENDER TEXT NOT NULL, "
"COURSE TEXT, "
"STATUS TEXT, "
"WAM REAL"
")"
我的插入功能是
void addStudent(sqlite3 *handle) {
char name[20], surname[20];
char street_name[20], suburb[20];
int street_no;
int postcode;
char gender[10];
char course[10];
char status[10];
float wam = 0.0;
int student_number;
struct tm *t = malloc(sizeof(struct tm));
int year;
int month;
int day;
char course_bool[10];
char dob[80];
fflush(stdin); /* kill any characters left in buffer */
printf("Enter student name: ");
scanf("%s", &name);
fflush(stdin);
printf("Enter student surname: ");
scanf("%s", &surname);
fflush(stdin);
printf("Enter student number: ");
scanf("%d", &student_number);
fflush(stdin);
printf("student DOB: \n");
printf("Enter day of dob:");
scanf("%d", &day);
fflush(stdin);
printf("Enter month of dob:");
scanf("%d", &month);
fflush(stdin);
printf("Enter yesr of dob:");
scanf("%d", &year);
fflush(stdin);
t->tm_year = year;
t->tm_mon = month;
t->tm_mday = day;
printf("Information about student address\n");
printf("Enter street number: ");
scanf("%d", &street_no);
fflush(stdin);
printf("Enter street name: ");
scanf("%s", &street_name);
fflush(stdin);
printf("Enter suburb: ");
scanf("%s", &suburb);
fflush(stdin);
printf("Enter postcode: ");
scanf("%d", &postcode);
fflush(stdin);
printf("Enter Student Gender: ");
scanf("%s", &gender);
fflush(stdin);
printf("Enter 'A' if student is active else enter 'N': ");
scanf("%s", &status);
fflush(stdin);
printf("does student have any course yes/no: ");
scanf("%s", &course_bool);
fflush(stdin);
if (strcmp(course_bool, "yes") == 0) {
printf("Enter student course code: ");
scanf("%s", &course);
fflush(stdin);
}
printf("Enter student WAM: ");
scanf("%f", &wam);
fflush(stdin);
// now validate inputs
//name should not null
int errors = 0;
if (strcmp(name, "") == 0) {
errors = 1;
printf("Student's name can not be empty\n");
}
if (student_number > 9999999) {
errors = 1;
printf("Student number can not should have maximum seven degits\n");
}
if (strcmp(street_name, "") == 0) {
errors = 1;
printf("Address of student should have street name\n");
}
if (strcmp(suburb, "") == 0) {
errors = 1;
printf("Address of student should have suturb name\n");
}
if (checkPostcode(postcode) != 0) {
errors = 1;
printf("Postcode is not valid\n");
}
if (checkCourseName(course) != 0) {
errors = 1;
printf("Course name is not valid\n");
}
/*
if (checkDOB(t) != 0) {
errors = 1;
printf("Date of birth is not valid\n");
}*/
if (errors == 0) {
strftime(dob, 80, "%d-%m-%Y", t);
//now delte t
free(t);
char sql[] =
"INSERT INTO STUDENTS VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?)";
sqlite3_stmt *stmt = NULL;
int retval = sqlite3_prepare_v2(handle, sql, -1, &stmt, NULL);
if (retval != SQLITE_OK) {
printf("Error while creating insert statement. '%s'",
sqlite3_errmsg(handle));
return;
}
sqlite3_bind_null(stmt, 1);
sqlite3_bind_text(stmt, 2, name, -1, SQLITE_STATIC);
sqlite3_bind_text(stmt, 3, surname, -1, SQLITE_STATIC);
sqlite3_bind_int(stmt, 4, street_no);
sqlite3_bind_text(stmt, 5, street_name, -1, SQLITE_STATIC);
sqlite3_bind_text(stmt, 6, suburb, -1, SQLITE_STATIC);
sqlite3_bind_int(stmt, 7, postcode);
sqlite3_bind_int(stmt, 8, student_number);
sqlite3_bind_text(stmt, 9, dob, -1, SQLITE_STATIC);
sqlite3_bind_text(stmt, 10, gender, -1, SQLITE_STATIC);
sqlite3_bind_text(stmt, 11, course, -1, SQLITE_STATIC);
sqlite3_bind_text(stmt, 12, status, -1, SQLITE_STATIC);
sqlite3_bind_double(stmt, 13, wam);
retval = sqlite3_step(stmt);
sqlite3_finalize(stmt);
sqlite3_exec(handle, "COMMIT", NULL, NULL, NULL);
//sqlite3_reset(stmt);
if (retval != SQLITE_DONE) {
printf("Error while insert student. '%s'", sqlite3_errmsg(handle));
return;
}
printf("Student added to system successfully\n");
} else {
printf("student not added to system\n");
}
}
但是当我列出数据条目时,它们的顺序不一样