无法使用SQLite保存数据

时间:2014-11-22 07:52:34

标签: android sqlite

我想使用SQLite将数据保存到数据库。但是下面的SaveSQLite类代码出现错误。

我在Fragment1类中编写InsertData(),可以成功保存。但是,当我在SaveSQLite类中编写InsertData()时发生错误。

如何在SaveSQLite类中运行InsertData()?

MySQLiteOpenHelper helper = new MySQLiteOpenHelper(this);
SQLiteDatabase db = helper.getWritableDatabase();

Fragment1.java

public class Fragment1 extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceBundle){
        View view = inflater.inflate(R.layout.fragment1, container, false);
        Button btn = (Button)view.findViewById(R.id.bt_save);
        btn.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                EditText et_name = (EditText)view.findViewById(R.id.et_name);
                String name = et_name.getText().toString();
                // Insert Data
                SaveSQLite sqlite = new SaveSQLite();
                sqlite.InsertData(name);  //(Fragment1.java:37)
            }
        }
        return view;
    }
}

SaveSQLite.java

public class SaveSQLite extends Activity {
    public SaveSQLite() {
    }
    public void InsertData(String name){
        MySQLiteOpenHelper helper = new MySQLiteOpenHelper(this);
        SQLiteDatabase db = helper.getWritableDatabase();  //(SaveSQLite.java:21)
        db.beginTransaction();
        ContentValues cv = new ContentValues();
        cv.put("name", name);
        db.insert("user", null, cv);
        db.setTransactionSuccessful();
        db.endTransaction();
        db.close();
        helper.close();
    }
}

MySQLiteOpenHelper.java

public class MySQLiteOpenHelper extends SQLiteOpenHelper {
    private final static String DB_NAME = "sample.db";
    private final static int VERSION = 1;

    public MySQLiteOpenHelper (Context context) {
        super(context, DB_NAME, null, VERSION);
    }
    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL("CREATE TABLE IF NOT EXISTS user(" +
            "_id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL," +
            "name TEXT)");
    }
    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    }
}

logcat的

E/AndroidRuntime(9206): FATAL EXCEPTION: main
E/AndroidRuntime(9206): Process: com.example.sample, PID: 9206
E/AndroidRuntime(9206): java.lang.NullPointerException
E/AndroidRuntime(9206):     at android.content.ContextWrapper.openOrCreateDatabase(ContextWrapper.java:256)
E/AndroidRuntime(9206):     at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:224)
E/AndroidRuntime(9206):     at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:164)
E/AndroidRuntime(9206):     at com.example.sample.SaveSQLite.InsertData(SaveSQLite.java:21)
E/AndroidRuntime(9206):     at com.example.sample.Fragment1$4.onClick(Fragment1.java:37)
E/AndroidRuntime(9206):     at android.view.View.performClick(View.java:4438)
E/AndroidRuntime(9206):     at android.view.View$PerformClick.run(View.java:18422)
E/AndroidRuntime(9206):     at android.os.Handler.handleCallback(Handler.java:733)
E/AndroidRuntime(9206):     at android.os.Handler.dispatchMessage(Handler.java:95)
E/AndroidRuntime(9206):     at android.os.Looper.loop(Looper.java:136)
E/AndroidRuntime(9206):     at android.app.ActivityThread.main(ActivityThread.java:5017)
E/AndroidRuntime(9206):     at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime(9206):     at java.lang.reflect.Method.invoke(Method.java:515)
E/AndroidRuntime(9206):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
E/AndroidRuntime(9206):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
E/AndroidRuntime(9206):     at dalvik.system.NativeStart.main(Native Method)

2 个答案:

答案 0 :(得分:0)

SaveSQLite sqlite = new SaveSQLite();更改为

SaveSQLite sqlite = new SaveSQLite(getActivity());
Fragment1中的

&安培;

更改类“SaveSQLite”的代码,例如

public class SaveSQLite {
    private Context mcontext;
    public SaveSQLite(Context context) {
      this.mcontext = context;
    }
    public void InsertData(String name){
        MySQLiteOpenHelper helper = new MySQLiteOpenHelper(mcontext);
        SQLiteDatabase db = helper.getWritableDatabase();  //(SaveSQLite.java:21)
        db.beginTransaction();
        ContentValues cv = new ContentValues();
        cv.put("name", name);
        db.insert("user", null, cv);
        db.setTransactionSuccessful();
        db.endTransaction();
        db.close();
        helper.close();
    }
}

希望它会帮助你

答案 1 :(得分:0)

使用此方法在数据库表中添加数据。

// table name
private static final String YOUR TABLE NAME = "name";

public void addItem(String title, String description) {
    SQLiteDatabase db = this.getWritableDatabase();

    ContentValues values = new ContentValues();

    values.put(KEY_TITLE, title); // news title
    values.put(KEY_DESCRIPTION, description); // news
                                                // description

    db.insert(YOUR TABLE NAME, null, values);
    db.close();

}

希望它可以帮助您解决问题。