0

found Error in creating simple SQLite databas Android Application

**03-12 12:57:39.635: E/Database(333): Failure 1 
(near "tablefriends": syntax error) on 0x29b2b0 
when preparing 'create tablefriends(_id integer 
primary key autoincrement,name text not null,address text);

when i write this code in my activity class, my application even did not run. but if i removed this code from activity class , my application run but it gives exception when i insert data into the database

protected void onPause() {
    // TODO Auto-generated method stub
    super.onPause();
    FriendsDataSource s=new FriendsDataSource(this);
    s.closedatabase();

}

@Override
protected void onResume() {
    // TODO Auto-generated method stub
    super.onResume();
    FriendsDataSource s=new FriendsDataSource(this);
    s.opendatabase();
}

public static final String FRIENDS_TABLE_CREATE_STMT="create table"+TABLE_NAME+
       "("+COLUMN_ID+" integer primary key autoincrement,"+
       COLUMN_FRIEND_NAME+" text not null,"+COLUMN_ADDRESS+" text);";
1
  • I'd like to add that you should rather create an object-scope instance of FriendsDataSource in onResume and close the database of that instance instead of creating two instances, like mFriendsSource = new FriendsDataSource(this); where mFriendsSource is a member of the class. Commented Mar 12, 2012 at 14:25

1 Answer 1

2

You've got problems with your spacing as so often happens when concatenating strings;

"create table" + TABLE_NAME // becomes "create tablefriends"

When ending up with strange SQL errors, it's always a good idea to look at/trace the generated string so that it matches what you expect. In this case, your error message right at the top of the question shows the error though.

The correct version should be;

"create table " + TABLE_NAME // becomes "create table friends"
Sign up to request clarification or add additional context in comments.

4 Comments

it still gives me an exception when i press button of "add friend"
@ZaidIqbal You're not showing any code or the exception for "Add friend" in this question, you may want to start a new question with all info you have on that.
03-12 16:56:25.652: E/Database(334): Failure 1 (near "tablefriends": syntax error) on 0x29b268 when preparing 'create tablefriends(_id integer primary key autoincrement,name text not null,address text);'.
i have debug the code and found exception in this line "long id=friendsDB.insert(FriendsMetaData.TABLE_NAME, null, newFriendValues); "

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.