-2

I read many tutorial but it is still difficult to me. Then I copy some to Eclipse to try and it doesn't work I check the file explore that the database didn't created. The followings is my code. please help and thanks.

It is the database helper.

package com.example.trydb;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
public class DBHelper extends SQLiteOpenHelper {
   public static final String TABLE_NAME = "friends"; 
   public static final String NAME = "name";
   public static final String TEL = "tel";
   public static final String EMAIL = "email";  
   private final static String DATABASE_NAME = "demo.db"; 
   private final static int DATABASE_VERSION = 1;  
   public DBHelper(Context context) {
       super(context, DATABASE_NAME, null, DATABASE_VERSION);
       Log.d("Database operations","Database created ");
  }
    @Override   
    public void onCreate(SQLiteDatabase db) {
        db.execSQL("CREATE TABLE " + TABLE_NAME + " (" + NAME + "CHAR," + TEL + "CHAR," +     EMAIL + "CHAR);");
        Log.d("Database operations","Database created successfully");
    }
    @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
            final String DROP_TABLE = "DROP TABLE IF EXISTS " + TABLE_NAME; 
            db.execSQL(DROP_TABLE);     
            onCreate(db);
    }
}

And it is the main activity.

package com.example.trydb;

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;


public class MainActivity extends ActionBarActivity {
    private DBHelper dbhelper ;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        dbhelper = new DBHelper(this);
        dbhelper.close();
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}
android fy
  • 15
  • 2
  • 1
    you missed spaces in your create query. (but that nothing you wouldn't have learned by simply taking the time to read the stacktrace.) – njzk2 Nov 12 '14 at 15:40
  • 1
    First of all - provide the log. After that - remove `dbhelper.close()`. **Why?** Read more about connection and using `SQLite` in **Android**. – Sergey Shustikov Nov 12 '14 at 15:41

4 Answers4

1

Try changing to this

    @Override   
    public void onCreate(SQLiteDatabase db) {
        String CREATE_TABLE = "CREATE TABLE " + TABLE_NAME + "("
        + NAME + " CHAR,"
        + TEL + " CHAR,"
        + EMAIL + " CHAR" 
        + ")";
        db.execSQL(CREATE_TABLE);
        Log.d("Database operations","Database created successfully");
    }
Galax
  • 367
  • 1
  • 13
0

You need to call getWritableDatabase() or getReadableDatabase() on the database helper to actually create the database. Instantiating the helper alone is not sufficient.

Further reading: When is SQLiteOpenHelper onCreate() / onUpgrade() run?

You probably also want to add whitespace between column names and their types.

Community
  • 1
  • 1
laalto
  • 150,114
  • 66
  • 286
  • 303
0

Try to add a space between the columns.

Haris Bjelic
  • 117
  • 1
  • 9
0

From the docs of method getWritibleDatabase();

Create and/or open a database that will be used for reading and writing. The first time this is called, the database will be opened and onCreate(SQLiteDatabase), onUpgrade(SQLiteDatabase, int, int) and/or onOpen(SQLiteDatabase) will be called.

So...

Once opened successfully, the database is cached, so you can call this method every time you need to write to the database. (Make sure to call close() when you no longer need the database.) Errors such as bad permissions or a full disk may cause this method to fail, but future attempts may succeed if the problem is fixed.

Sergey Shustikov
  • 15,377
  • 12
  • 67
  • 119