title: Run-time error in SQLite Database
body: <p>I've immplemented an SQLite database here. The database consists of objects of class Contact. However my app is crashing as soon as it starts. Here's the Logcat and the code. </p>
  
  <p><strong>Logcat</strong></p>
  
  <pre><code>FATAL EXCEPTION: main
  Process: com.example.nirvan.sqliteexample, PID: 30065
  java.lang.RuntimeException: Unable to start activity ComponentInfo {
    com.example.nirvan.sqliteexample / com.example.nirvan.sqliteexample.MainActivity
   }: android.database.sqlite.SQLiteException: near "TABLEcontacts": syntax error(code 1): ,
   while compiling: CREATE TABLEcontacts(idINTEGER KEYname TEXT)
  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java: 2198)
  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java: 2257)
  at android.app.ActivityThread.access$800(ActivityThread.java: 139)
  at android.app.ActivityThread$H.handleMessage(ActivityThread.java: 1210)
  at android.os.Handler.dispatchMessage(Handler.java: 102)
  at android.os.Looper.loop(Looper.java: 136)
  at android.app.ActivityThread.main(ActivityThread.java: 5086)
  at java.lang.reflect.Method.invokeNative(Native Method)
  at java.lang.reflect.Method.invoke(Method.java: 515)
  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java: 785)
  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java: 601)
  at dalvik.system.NativeStart.main(Native Method)
  Caused by: android.database.sqlite.SQLiteException: near "TABLEcontacts": syntax error(code 1): ,
   while compiling: CREATE TABLEcontacts(idINTEGER KEYname TEXT)
  at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
  at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java: 889)
  at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java: 500)
  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: 1672)
  at android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java: 1603)
  at com.example.nirvan.sqliteexample.DatabaseHandler.onCreate(DatabaseHandler.java: 28)
  at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java: 252)
  at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java: 164)
  at com.example.nirvan.sqliteexample.DatabaseHandler.addContact(DatabaseHandler.java: 40)
  at com.example.nirvan.sqliteexample.MainActivity.onCreate(MainActivity.java: 21)
  at android.app.Activity.performCreate(Activity.java: 5248)
  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java: 1110)
  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java: 2162)
  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java: 2257) 
  at android.app.ActivityThread.access$800(ActivityThread.java: 139) 
  at android.app.ActivityThread$H.handleMessage(ActivityThread.java: 1210) 
  
  FATAL EXCEPTION: main
  Process: com.example.nirvan.sqliteexample, PID: 32547
  java.lang.RuntimeException: Unable to start activity ComponentInfo {
    com.example.nirvan.sqliteexample / com.example.nirvan.sqliteexample.MainActivity
   }: android.database.sqlite.SQLiteException: near "TABLEcontacts": syntax error(code 1): ,
   while compiling: CREATE TABLEcontacts(idINTEGER KEYname TEXT)
  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java: 2198)
  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java: 2257)
  at android.app.ActivityThread.access$800(ActivityThread.java: 139)
  at android.app.ActivityThread$H.handleMessage(ActivityThread.java: 1210)
  at android.os.Handler.dispatchMessage(Handler.java: 102)
  at android.os.Looper.loop(Looper.java: 136)
  at android.app.ActivityThread.main(ActivityThread.java: 5086)
  at java.lang.reflect.Method.invokeNative(Native Method)
  at java.lang.reflect.Method.invoke(Method.java: 515)
  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java: 785)
  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java: 601)
  at dalvik.system.NativeStart.main(Native Method)
  Caused by: android.database.sqlite.SQLiteException: near "TABLEcontacts": syntax error(code 1): ,
   while compiling: CREATE TABLEcontacts(idINTEGER KEYname TEXT)
  at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
  at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java: 889)
  at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java: 500)
  </code></pre>
  
  <p><strong>MainActivity.java</strong></p>
  
  <pre><code>package com.example.nirvan.sqliteexample;
  
  import android.support.v7.app.AppCompatActivity;
  import android.os.Bundle;
  import android.util.Log;
  import android.widget.TextView;
  
  public class MainActivity extends AppCompatActivity
  {
  
  @Override
  protected void onCreate(Bundle savedInstanceState)
  {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
  
      DatabaseHandler db = new DatabaseHandler(this);
  
      // Inserting Contacts
      //Log.d("Insert: ", "Inserting ..");
      db.addContact(new Contact(1, "one"));
      db.addContact(new Contact(2, "two"));
      db.addContact(new Contact(3, "three"));
  
  
      TextView text=(TextView) findViewById(R.id.text);
      Contact cn;
      cn=db.getContact(2);
      text.setText(cn.name);
  }
  
  
  
  }
  </code></pre>
  
  <p><strong>Contact.java</strong></p>
  
  <pre><code>public class Contact
  {
  public String name;
  public int id;
  
  Contact(int id, String name)
  {
      this.id=id;
      this.name=name;
  }
  
  
  }
  </code></pre>
  
  <p><strong>DatabaseHandler.java</strong></p>
  
  <pre><code>import android.content.ContentValues;
  import android.content.Context;
  import android.database.Cursor;
  import android.database.sqlite.SQLiteDatabase;
  import android.database.sqlite.SQLiteOpenHelper;
  
  public class DatabaseHandler extends SQLiteOpenHelper
  {
   private static final int DATABASE_VERSION=1;
   private static final String DATABASE_NAME="contactsManager";
   private static final String TABLE_CONTACTS="contacts";
   private static final String KEY_ID="id";
   private static final String KEY_NAME="name";
  
  public DatabaseHandler(Context context)
  {
      super(context, DATABASE_NAME, null, DATABASE_VERSION);}
  
  @Override
  public void onCreate(SQLiteDatabase db)
  {
      String CREATE_CONTACTS_TABLE="CREATE TABLE"+TABLE_CONTACTS+"("+KEY_ID+"INTEGER PRIMARY KEY"+KEY_NAME+" TEXT"+")";
      db.execSQL(CREATE_CONTACTS_TABLE);
  }
  
  @Override
  public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
  {
   db.execSQL("DROP TABLE IF EXISTS"+TABLE_CONTACTS);
      onCreate(db);
  }
  
  void addContact(Contact contact)
  {
      SQLiteDatabase db = this.getWritableDatabase();
  
      ContentValues values = new ContentValues();
      values.put(KEY_NAME, contact.name);
      values.put(KEY_ID, contact.id);
  
      // Inserting Row
      db.insert(TABLE_CONTACTS, null, values);
      //2nd argument is String containing nullColumnHack
      db.close(); // Closing database connection
  }
  
  Contact getContact(int id)
  {
      SQLiteDatabase db = this.getReadableDatabase();
  
      Cursor cursor = db.query(TABLE_CONTACTS, new String[] { KEY_ID,
                      KEY_NAME}, KEY_ID + "=?",
              new String[] { String.valueOf(id) }, null, null, null, null);
      if (cursor != null)
          cursor.moveToFirst();
  
      Contact contact = new Contact(Integer.parseInt(cursor.getString(0)),
              cursor.getString(1));
      // return contact
      return contact;
  }
  
  public int updateContact(Contact contact)
  {
      SQLiteDatabase db = this.getWritableDatabase();
  
      ContentValues values = new ContentValues();
      values.put(KEY_NAME, contact.name);
  
  
      // updating row
      return db.update(TABLE_CONTACTS, values, KEY_ID + " = ?",
              new String[] { String.valueOf(contact.id) });
  }
  
  
  }
  </code></pre>
  
  <p><strong>activityMain.xml</strong></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:tools="http://schemas.android.com/tools"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:paddingBottom="@dimen/activity_vertical_margin"
  android:paddingLeft="@dimen/activity_horizontal_margin"
  android:paddingRight="@dimen/activity_horizontal_margin"
  android:paddingTop="@dimen/activity_vertical_margin"
  tools:context="com.example.nirvan.sqliteexample.MainActivity"&gt;
  
  &lt;TextView
      android:id="@+id/text"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="Hello World!" /&gt;
  &lt;/RelativeLayout&gt;
  </code></pre>