title: How to search in a SQLite database in android?
body: <p>I'm making a database for storing the information of the contacts. Because I'm new to android, I used a code found from the Internet. However, it has errors and when run it app crashes.</p>
  <p>Here is the code for database :</p>
  <h3>Storage Manipulator</h3>
  <pre><code>public class FriendStorage extends SQLiteOpenHelper {
  
  public FriendStorage(Context context, String name, SQLiteDatabase.CursorFactory factory, int version){
      super(context, name, factory, version);
  }
  
  public static final String Database_name = &quot;net.Friend.db&quot;;
  public static final int Database_version = 1;
  
  public static final String _ID = &quot;_id&quot;;
  public static final String Table_Friend = &quot;Table_friend&quot;;
  public static final String Username = &quot;username&quot;;
  public static final String PhoneNumber = &quot;phoneNumber&quot;;
  
  private static final String TABLE_FRIEND_CREATE =
          &quot;CREATE TABLE &quot; + Table_Friend
                  + &quot; (&quot; + _ID + &quot; INTEGER PRIMARY KEY AUTOINCREMENT, &quot;
                  + Username + &quot; VARCHAR(25), &quot;
                  + PhoneNumber + &quot; VARCHAR(25)&quot;;
  
  public static final String Table_Friend_Drop = &quot;DROP TABLE IF EXISTS&quot; + Table_Friend;
  
  @Override
  public void onCreate(SQLiteDatabase db) {
      db.execSQL(TABLE_FRIEND_CREATE);
  }
  
  public void insert(String username, String phoneNumber){
      long rowId;
      try {
  
          SQLiteDatabase db = getWritableDatabase();
          ContentValues contentValues = new ContentValues();
          contentValues.put(Username, username);
          contentValues.put(PhoneNumber, phoneNumber);
          rowId = db.insert(Table_Friend, null, contentValues);
  
      }catch (Exception e){
          e.printStackTrace();
      }
  }
  
  public Cursor get(){
      Cursor cursor;
      SQLiteDatabase db = getReadableDatabase();
      String selectQuery = &quot;SELECT * FROM&quot; + Table_Friend;
      cursor = db.rawQuery(selectQuery, null);
      return cursor;
  }
  
  public Cursor Update(String preUsername, String prePhoneNumber, String newUsername, String newPhoneNumber){
      Cursor cursor;
      SQLiteDatabase db = getWritableDatabase();
      String updateTable = &quot;UPDATE&quot; + Table_Friend + &quot;SET&quot; + Username + &quot; = &quot; + newUsername
              + &quot;AND&quot; + PhoneNumber + &quot; = &quot; + newPhoneNumber + &quot;WHERE&quot; + Username + &quot; = &quot;
              + preUsername + &quot;AND&quot; + PhoneNumber + &quot; = &quot; + prePhoneNumber;
      cursor = db.rawQuery(updateTable, null);
      return cursor;
  }
  
  @Override
  public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
      db.execSQL(Table_Friend_Drop);
  }
  
  }
  </code></pre>
  <p>and in the Main Activity i use <strong>get</strong> function to search in database.</p>
  <h3>MainActivity</h3>
  <pre><code>FriendStorage friendInfoStorage = new FriendStorage(this, &quot;net.Friend.db&quot;, null, 1);
  cursor = friendInfoStorage.get();
  int noOfFriends = 0;
  if (cursor.getCount() &gt; 0) {
      cursor.moveToFirst();
      while ((!cursor.isAfterLast()) &amp;&amp; (noOfFriends &lt; cursor.getCount())) {
          if (noOfFriends == 0) {
              Myself.setUsername(cursor.getString(1));
              Myself.setPhone_Number(cursor.getString(2));
          } else {
              User.setUsername(cursor.getString(1));
              User.setPhone_Number(cursor.getString(2));
              Friends.add(User);
          }
          noOfFriends++;
          cursor.moveToNext();
      }
  }
  friendInfoStorage.close();
  </code></pre>
  <p><strong>Error is this :</strong></p>
  <pre><code>07-12 03:11:14.024 14157-14157/net.network.alire.clientchat E/AndroidRuntime: FATAL EXCEPTION: main
                                                                            Process: net.network.alire.clientchat, PID: 14157
                                                                            java.lang.RuntimeException: Unable to start activity ComponentInfo{net.network.alire.clientchat/net.network.alire.clientchat.MainActivity}: android.database.sqlite.SQLiteException: near &quot;)&quot;: syntax error (code 1): , while compiling: CREATE TABLE Table_friend (_id INTEGER PRIMARY KEY AUTOINCREMENT, username VARCHAR(25), phoneNumber VARCHAR(25)
                                                                                at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2329)
                                                                                at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2389)
                                                                                at android.app.ActivityThread.access$900(ActivityThread.java:147)
                                                                                at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1296)
                                                                                at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                                at android.os.Looper.loop(Looper.java:135)
                                                                                at android.app.ActivityThread.main(ActivityThread.java:5254)
                                                                                at java.lang.reflect.Method.invoke(Native Method)
                                                                                at java.lang.reflect.Method.invoke(Method.java:372)
                                                                                at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:898)
                                                                                at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:693)
                                                                             Caused by: android.database.sqlite.SQLiteException: near &quot;)&quot;: syntax error (code 1): , while compiling: CREATE TABLE Table_friend (_id INTEGER PRIMARY KEY AUTOINCREMENT, username VARCHAR(25), phoneNumber VARCHAR(25)
                                                                                at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
                                                                                at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:891)
                                                                                at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:502)
                                                                                at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
                                                                                at android.database.sqlite.SQLiteProgram.&lt;init&gt;(SQLiteProgram.java:58)
                                                                                at android.database.sqlite.SQLiteStatement.&lt;init&gt;(SQLiteStatement.java:31)
                                                                                at android.database.sqlite.SQLiteDatabase.executeSql(SQLiteDatabase.java:1674)
                                                                                at android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:1605)
                                                                                at net.network.alire.toolBox.FriendStorage.onCreate(FriendStorage.java:36)
                                                                                at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:251)
                                                                                at android.database.sqlite.SQLiteOpenHelper.getReadableDatabase(SQLiteOpenHelper.java:187)
                                                                                at net.network.alire.toolBox.FriendStorage.get(FriendStorage.java:56)
                                                                                at net.network.alire.clientchat.MainActivity.onCreate(MainActivity.java:52)
                                                                                at android.app.Activity.performCreate(Activity.java:5933)
                                                                                at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
                                                                                at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2282)
                                                                                at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2389) 
                                                                                at android.app.ActivityThread.access$900(ActivityThread.java:147) 
                                                                                at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1296) 
                                                                                at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                                at android.os.Looper.loop(Looper.java:135) 
                                                                                at android.app.ActivityThread.main(ActivityThread.java:5254) 
                                                                                at java.lang.reflect.Method.invoke(Native Method) 
                                                                                at java.lang.reflect.Method.invoke(Method.java:372) 
                                                                                at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:898) 
                                                                                at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:693)
  </code></pre>
  <p>Can anyone tell me what is wrong with the code?</p>
  <p><strong>thanks in advance</strong></p>