Newbie89 Posted April 2, 2013 Share Posted April 2, 2013 (edited) How to insert data in the last row but not in the first row using C language.Do sqlite have function that control the row of the insert?I want the sequential row numbers from insert query.Thanks. Edited April 2, 2013 by Newbie89 Link to comment Share on other sites More sharing options...
justsomeguy Posted April 2, 2013 Share Posted April 2, 2013 When you insert a record it is always the "last" row. Insert queries do not change existing data. Insert queries add data. Update queries change data. Select queries get data. You might also be looking for this: http://www.sqlite.org/autoinc.html 1 Link to comment Share on other sites More sharing options...
Newbie89 Posted April 3, 2013 Author Share Posted April 3, 2013 (edited) Can help me see the problem?Below is my code Create.sql create table student (No integer primary key autoincrement, Date1 varchar(20), Time varchar(10),Name varchar(10), Id_no integer); insert into student (Date1 , Time, Name, Id_no) values ('10/10/2012', '9.35am', 'Kean Heng', '091021434'); insert.c # include <stdio.h> # include <sqlite3.h> # include <stdlib.h> int main(void) { sqlite3 *conn; sqlite3_stmt *res; int error = 0; int rec_count = 0; const char *errMSG; const char *tail; char sql_lite[900]=" "; char name[50]; int date=0,month=0,year=0,matrix_number=0,number,x; float time =0; error = sqlite3_open("hong.sl3", &conn); if (error) { printf("Can not open database"); } printf("Enter the number of data to be inserted\n"); scanf("%d",&x); for(number=x;number>0;number--) { printf("Enter the name of the student for insert\n"); scanf("%s",name); printf("Enter the date, month, year of the student\n"); scanf("%d%d%d",&date,&month,&year); printf("Enter the time \n"); scanf("%f",&time); printf("Enter the matrix number of the student for insert\n"); scanf("%d",&matrix_number); } sprintf(sql_lite, "insert into student values ('%d/%d/%d','%.2fam','%s',%d);",date,month,year,time,name,matrix_number); error = sqlite3_exec(conn, sql_lite, 0, 0, 0); error = sqlite3_prepare_v2(conn, "select * from student order by No",1000, &res, &tail); if (error != SQLITE_OK) { printf("We did not get any data!"); exit(0); } printf("=======================================\n"); while (sqlite3_step(res) == SQLITE_ROW) { printf("%d|", sqlite3_column_int(res, 0)); printf("%s|", sqlite3_column_text(res, 1)); printf("%s|", sqlite3_column_text(res, 2)); printf("%s|", sqlite3_column_text(res, 3)); printf("%u\n", sqlite3_column_int(res, 4)); rec_count++; } printf("=======================================\n"); printf("We received %d records.\nTotal rows=%d\n", rec_count,SQLITE_ROW); sqlite3_finalize(res); sqlite3_close(conn); return 0; } Edited April 3, 2013 by Newbie89 Link to comment Share on other sites More sharing options...
Newbie89 Posted April 3, 2013 Author Share Posted April 3, 2013 (edited) My problem now is I can get the create.sql from c language,but I can't insert the data in the database. Edited April 3, 2013 by Newbie89 Link to comment Share on other sites More sharing options...
Newbie89 Posted April 3, 2013 Author Share Posted April 3, 2013 Below is my output: hong@ubuntu:~/Check$ sqlite3 hong.sl3<create.sqlhong@ubuntu:~/Check$ gcc -o insert insert.c -lsqlite3hong@ubuntu:~/Check$ ./insertEnter the number of data to be inserted1Enter the name of the student for insertJohnEnter the date, month, year of the student12 12 2012Enter the time12.00Enter the matrix number of the student for insert091232432=======================================1|10/10/2012|9.35am|Kean Heng|91021434=======================================We received 1 records.Total rows=100hong@ubuntu:~/Check$ Link to comment Share on other sites More sharing options...
justsomeguy Posted April 8, 2013 Share Posted April 8, 2013 Print out the insert query you're building. It looks like the format string you're sending to scanf to read the date doesn't match the input. Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now