无法将值插入SQLite数据库?

时间:2017-10-08 17:09:30

标签: android sqlite insert android-sqlite sql-insert

Java编码/ Android应用程序开发的新手。我正在尝试创建一个应用程序,它将接收信息数据并使用SQLite来管理它。现在,当我尝试添加信息时,没有输入任何内容,当我查看数据库时,我得到“未找到数据”。

这是我DatabaseHelper中的代码:

public class DatabaseHelper extends SQLiteOpenHelper {
    public static final String DATABASE_NAME = "database.db";
    public static final String TABLE_NAME = "userinfo";
    public static final String COL_1 = "AGE";
    public static final String COL_2 = "SEX";
    public static final String COL_3 = "HEIGHT";
    public static final String COL_4 = "WEIGHT";
    public static final String COL_5 = "TYPE";
    public static final String COL_6 = "ACTIVITY";
    public String str = null;

    public SQLiteDatabase database;
    public DatabaseHelper db;

    public DatabaseHelper(Context context) {
        super(context, DATABASE_NAME, null, 1);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL("create table " + TABLE_NAME + " (AGE INTEGER, SEX TEXT, HEIGHT INTEGER, WEIGHT INTEGER, TYPE INTEGER, ACTIVITY INTEGER)");

    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
        onCreate(db);
    }

    public boolean insertData(Integer age, String sex) {
        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues contentValues = new ContentValues();
        contentValues.put(COL_1, age);
        contentValues.put(COL_2, sex);
        long result = database.insert(TABLE_NAME, null, contentValues);

        if(result == -1)
            return false;
        else
            return true;
    }

    public boolean insertData2(Integer height, Integer weight, Integer type) {
        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues contentValues = new ContentValues();
        contentValues.put(COL_3,height);
        contentValues.put(COL_4,weight);
        contentValues.put(COL_5,type);
        long result = db.insert(TABLE_NAME, null, contentValues);
        if(result == -1)
            return false;
        else
            return true;
    }

    public boolean insertData3(Integer activity) {
        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues contentValues = new ContentValues();
        contentValues.put(COL_6,activity);
        long result = db.insert(TABLE_NAME, null, contentValues);
        if(result == -1)
            return false;
        else
            return true;
    }

    public Cursor getAllData() {
        SQLiteDatabase db = this.getWritableDatabase();
        Cursor res = db.rawQuery("select * from " + TABLE_NAME, null);
        return res;
    }
}

三个不同的“InsertData”用于三个需要向数据库添加数据的活动。这是正确的方法吗?此处还有调用第一个“InsertData”的活动。

public class info1 extends AppCompatActivity {

    DatabaseHelper myDb;
    EditText editText13,editText14;
    Button btnAddData;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_info1);

        editText13 = (EditText)findViewById(R.id.editText13);
        editText14 = (EditText)findViewById(R.id.editText14);
        btnAddData = (Button)findViewById(R.id.button8);
    }

    public void AddData() {
        btnAddData.setOnClickListener(
                new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        boolean isInserted = myDb.insertData(Integer.parseInt(editText13.getText().toString()),editText14.getText().toString() );
                        if(isInserted == true)
                            Toast.makeText(info1.this, "Data Inserted",Toast.LENGTH_LONG).show();
                        else
                            Toast.makeText(info1.this, "Data Not Inserted",Toast.LENGTH_LONG).show();
                    }
                }
        );
    }

    public void NextPage2(View view) {
        Intent i = new Intent(info1.this, info2.class);
        startActivity(i);
    }
}

任何帮助将不胜感激!我已经待了好几个小时,但我不知道要改变什么......

1 个答案:

答案 0 :(得分:0)

您似乎没有调用AddData方法,因此未添加onClickListener。

行后

        btnAddData = (Button)findViewById(R.id.button8);

添加新行

        AddData();