无法在android中插入SQLIte的值

时间:2014-04-18 08:21:16

标签: java android sqlite

我正在使用一个简单的演示程序,我正在创建数据库,然后是一个表并尝试向表中添加值

我能做什么 :: 我能够在sqlite中创建表

我无法做什么 :: 我无法在其中插入值值

MainActivity.java

public class MainActivity extends Activity{

    Button addUser;
    SqliteAdapter helper;
    EditText usrName,pwd;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        helper=new SqliteAdapter(this);
        addUser=(Button) findViewById(R.id.button1);
        usrName=(EditText) findViewById(R.id.editText1);
        pwd=(EditText)findViewById(R.id.editText2);

    }


    public void addUser(View view){
        Log.d("LOG-MSG", "Onclick detected");
        helper.insertData(usrName.getText().toString(), pwd.getText().toString());
    }
}

SqliteAdapter.java

public class SqliteAdapter{

    SqliteHelper helper;
    ContentValues cv;
    SQLiteDatabase db;
    public SqliteAdapter(Context context){
        helper=new SqliteHelper(context);
    }

    public void insertData(String name,String password){

        db=helper.getWritableDatabase();
        Log.d("LOG-MSG", "insertDataMethod Entry");
        try {
            cv.put(SqliteHelper.NAME, name);
            Log.d("LOG-MSG", "Name inserted");
            cv.put(SqliteHelper.PASSWORD, password);
            Log.d("LOG-MSG", "password inserted");
        } catch (Exception e) {
            // TODO Auto-generated catch block
            Log.d("LOG-MSG-EXCEPTION", e.toString());
        }
        db.insert(SqliteHelper.TABLE_NAME, null, cv);
    }

    static class SqliteHelper extends SQLiteOpenHelper{

        private Context context;
        private static final String DATABASE_NAME="MyDatabase";
        private final static String TABLE_NAME="MyTable";
        private static final int DATABASE_VERSION=1;
        private final static String ID="_id";
        private final static String NAME="name";
        private final static String PASSWORD="password";
        private static final String CREATE_TABLE="CREATE TABLE "+TABLE_NAME+"("+ID+" INTEGER PRIMARY KEY AUTOINCREMENT, "+NAME+" VARCHAR(225), "+PASSWORD+" VARCHAR(225));";
        private static final String DROP_TABLE="DROP TABLE IF EXISTS "+TABLE_NAME+"";

        public SqliteHelper(Context context) {
            super(context, DATABASE_NAME, null, DATABASE_VERSION);
            this.context=context;
            Log.d("LOG-MSG", "Constructor Called");
            Message.message(context, "Constructor Called");
        }

        @Override
        public void onCreate(SQLiteDatabase db) {
            Log.d("LOG-MSG", "OnCreate Called");
            Message.message(context, "OnCreate Called");
            try {
                db.execSQL(CREATE_TABLE);
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                Log.d("LOG-MSG-onCreate", e.toString());
            }
        }

        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
            try {
                db.execSQL(DROP_TABLE);
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                Log.d("LOG-MSG-onUpgrade", e.toString());
                Message.message(context, "onUpgrade Called");
            }
            onCreate(db);
        }
    }   
}

日志 ::

04-18 13:45:19.567: D/LOG-MSG(3371): Constructor Called
04-18 13:45:20.007: D/gralloc_goldfish(3371): Emulator without GPU emulation detected.
04-18 13:45:20.107: W/TextLayoutCache(3371): computeValuesWithHarfbuzz -- need to force to single run
04-18 13:46:22.747: D/LOG-MSG(3371): Onclick detected
04-18 13:46:22.809: D/LOG-MSG(3371): insertDataMethod Entry
04-18 13:46:22.809: D/LOG-MSG-EXCEPTION(3371): java.lang.NullPointerException
04-18 13:46:22.817: I/SqliteDatabaseCpp(3371): sqlite returned: error code = 1, msg = near "null": syntax error, db=/data/data/com.example.sqlitedatabasedemo/databases/MyDatabase

{编辑后}

MainActivity.java

public class MainActivity extends Activity{

    Button addUser;
    SqliteAdapter helper;
    EditText usrName,pwd;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        helper=new SqliteAdapter(this);
        addUser=(Button) findViewById(R.id.button1);
        usrName=(EditText) findViewById(R.id.editText1);
        pwd=(EditText)findViewById(R.id.editText2); 
    }


    public void addUser(View view){
        Log.d("LOG-MSG", "Onclick detected");
        long recievedValue=helper.insertData(usrName.getText().toString(), pwd.getText().toString());
        if(recievedValue<0){
            Log.d("LOG-MSG", "NotInserted");
        }else
        {
            Log.d("LOG-MSG", "Inserted");           
        }
    }
}

SqliteAdapter.java

public class SqliteAdapter{

    SqliteHelper helper;
    ContentValues cv;
    SQLiteDatabase db;
    public SqliteAdapter(Context context){
        helper=new SqliteHelper(context);
    }

    public long insertData(String name,String password){

        db=helper.getWritableDatabase();
        Log.d("LOG-MSG", "insertDataMethod Entry");
        cv=new ContentValues();
        cv.put(SqliteHelper.NAME, name);
        Log.d("LOG-MSG", "Name="+name+"inserted");
        cv.put(SqliteHelper.PASSWORD, password);
        Log.d("LOG-MSG", "password="+password+"inserted");
        long result=db.insert(SqliteHelper.TABLE_NAME, null, cv);
        return result;
    }

    static class SqliteHelper extends SQLiteOpenHelper{

        private Context context;
        private static final String DATABASE_NAME="MyDatabase";
        private final static String TABLE_NAME="MyTable";
        private static final int DATABASE_VERSION=1;
        private final static String ID="_id";
        private final static String NAME="name";
        private final static String PASSWORD="password";
        private static final String CREATE_TABLE="CREATE TABLE "+TABLE_NAME+"("+ID+" INTEGER PRIMARY KEY AUTOINCREMENT, "+NAME+" VARCHAR(225), "+PASSWORD+" VARCHAR(225));";
        private static final String DROP_TABLE="DROP TABLE IF EXISTS "+TABLE_NAME+"";

        public SqliteHelper(Context context) {
            super(context, DATABASE_NAME, null, DATABASE_VERSION);
            this.context=context;
            Log.d("LOG-MSG", "Constructor Called");
            Message.message(context, "Constructor Called");
        }

        @Override
        public void onCreate(SQLiteDatabase db) {
            Log.d("LOG-MSG", "OnCreate Called");
            Message.message(context, "OnCreate Called");
            db.execSQL(CREATE_TABLE);
        }

        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
            db.execSQL(DROP_TABLE);
            onCreate(db);
        }
    }   
}

日志 ::

04-18 14:21:59.937: D/LOG-MSG(13661): Onclick detected
04-18 14:22:00.337: D/LOG-MSG(13661): insertDataMethod Entry
04-18 14:22:00.337: D/LOG-MSG(13661): Name=usainserted
04-18 14:22:00.337: D/LOG-MSG(13661): password=obamainserted
04-18 14:22:00.377: D/LOG-MSG(13661): Inserted

问题::当我检查DDMS ....我可以看到数据库。当我在查询浏览器中看到它没有插入值....为什么会发生这种情况...我也尝试调试可能的日志语句

3 个答案:

答案 0 :(得分:2)

您尚未初始化ContentValues cv对象。您抓住了NPE,然后继续尝试插入null ContentValues引用,导致终止该应用的异常。

  • cv = new ContentValues()添加到您的插入代码中。

  • 不要抓住Exception。大多数时候,它只是隐藏了你应该解决的问题。

  • 同样在onCreate()onUpgrade()中,您不应该抓住SQLException,而只是让它传播给来电者。

答案 1 :(得分:1)

似乎你没有制作ContentValues的内存。尝试按以下方式进行操作

 public void insertData(String name,String password){

    db=helper.getWritableDatabase();
    Log.d("LOG-MSG", "insertDataMethod Entry");
    try {
        cv = new ContentValues ();

        cv.put(SqliteHelper.NAME, name);
        Log.d("LOG-MSG", "Name inserted");
        cv.put(SqliteHelper.PASSWORD, password);
        Log.d("LOG-MSG", "password inserted");
    } catch (Exception e) {
        // TODO Auto-generated catch block
        Log.d("LOG-MSG-EXCEPTION", e.toString());
    }
    db.insert(SqliteHelper.TABLE_NAME, null, cv);
}

答案 2 :(得分:1)

嗨,您有一个问题,即ContentValues cv未正确实例化。

请添加第cv = new ContentValues ();

使用以下代码

public class SqliteAdapter{

    SqliteHelper helper;
    ContentValues cv;
    SQLiteDatabase db;
    public SqliteAdapter(Context context){
        helper=new SqliteHelper(context);
    }

    public void insertData(String name,String password){

        db=helper.getWritableDatabase();
cv = new ContentValues ();
        Log.d("LOG-MSG", "insertDataMethod Entry");
        try {
            cv.put(SqliteHelper.NAME, name);
            Log.d("LOG-MSG", "Name inserted");
            cv.put(SqliteHelper.PASSWORD, password);
            Log.d("LOG-MSG", "password inserted");
        } catch (Exception e) {
            // TODO Auto-generated catch block
            Log.d("LOG-MSG-EXCEPTION", e.toString());
        }
        db.insert(SqliteHelper.TABLE_NAME, null, cv);
    }

    static class SqliteHelper extends SQLiteOpenHelper{

        private Context context;
        private static final String DATABASE_NAME="MyDatabase";
        private final static String TABLE_NAME="MyTable";
        private static final int DATABASE_VERSION=1;
        private final static String ID="_id";
        private final static String NAME="name";
        private final static String PASSWORD="password";
        private static final String CREATE_TABLE="CREATE TABLE "+TABLE_NAME+"("+ID+" INTEGER PRIMARY KEY AUTOINCREMENT, "+NAME+" VARCHAR(225), "+PASSWORD+" VARCHAR(225));";
        private static final String DROP_TABLE="DROP TABLE IF EXISTS "+TABLE_NAME+"";

        public SqliteHelper(Context context) {
            super(context, DATABASE_NAME, null, DATABASE_VERSION);
            this.context=context;
            Log.d("LOG-MSG", "Constructor Called");
            Message.message(context, "Constructor Called");
        }

        @Override
        public void onCreate(SQLiteDatabase db) {
            Log.d("LOG-MSG", "OnCreate Called");
            Message.message(context, "OnCreate Called");
            try {
                db.execSQL(CREATE_TABLE);
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                Log.d("LOG-MSG-onCreate", e.toString());
            }
        }

        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
            try {
                db.execSQL(DROP_TABLE);
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                Log.d("LOG-MSG-onUpgrade", e.toString());
                Message.message(context, "onUpgrade Called");
            }
            onCreate(db);
        }
    }   
}