在Android上将数据保存到SQL中

时间:2015-12-01 18:55:44

标签: android sql

我正在尝试学习使用SQL与android。我试图将数据保存在editText字段的一列中。虽然我按照了一些教程并研究了And.Developers页面,但每次我尝试保存记录时(当我按下按钮时)我的应用程序都会崩溃。你能告诉我我做错了什么吗?代码如下..任何帮助都将非常感激。

主要活动

public class MainActivity extends AppCompatActivity {

    EditText editText;
    Button button;
    Context context;
    SQLiteDatabase sqLiteDatabase;
    DbHelper dbHelper;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        editText = (EditText) findViewById(R.id.editText);
        button = (Button) findViewById(R.id.button);


    }

    public void ulozit (View view){
        String darek = editText.getText().toString();
        dbHelper = new DbHelper(context);
        sqLiteDatabase = dbHelper.getWritableDatabase();
        dbHelper.addInformation(darek, sqLiteDatabase);
        Toast.makeText(getBaseContext(), "Tadáá", Toast.LENGTH_LONG).show();
        dbHelper.close();
    } 
}

具有表格布局的类

public  class contract { 

    public static abstract class rozvrzeni_tabulky {

        public static final String DAREK = "Dárek";
        public static final String TABLE_NAME = "vstupy";
    }
}

DB Class

public class DbHelper extends SQLiteOpenHelper {

    private static final String JMENO_DATABAZE = "Databáze";
    private static final int VERZE_DATABAZE = 1;
    private static final String VYTVOR_DOTAZ = "CREATE TABLE "+contract.rozvrzeni_tabulky.TABLE_NAME+"("+contract.rozvrzeni_tabulky.DAREK+" TEXT);";

    public DbHelper (Context context){
        super(context,JMENO_DATABAZE,null,VERZE_DATABAZE); //konstruktor k vytvoření databáíze
    }

    @Override
    public void onCreate(SQLiteDatabase db) {

        db.execSQL(VYTVOR_DOTAZ);
    }

    public void addInformation (String darek, SQLiteDatabase db){

        ContentValues contentValues = new ContentValues();
        contentValues.put(contract.rozvrzeni_tabulky.DAREK,darek);
        db.insert(JMENO_DATABAZE,null,contentValues);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

    }
}

2 个答案:

答案 0 :(得分:1)

“上下文上下文;”将其更改为Context context = this; 愿这会奏效。 附:对不起,如果不是......我也是新手

答案 1 :(得分:0)

没有必要在Activity中创建单独的Context引用,在这种情况下,您忘记初始化它,这可能是导致崩溃的原因。

只需删除context成员变量,然后使用this

public class MainActivity extends AppCompatActivity {

    EditText editText;
    Button button;
    //Context context;
    SQLiteDatabase sqLiteDatabase;
    DbHelper dbHelper;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        editText = (EditText) findViewById(R.id.editText);
        button = (Button) findViewById(R.id.button);


    }

    public void ulozit (View view){
        String darek = editText.getText().toString();
        dbHelper = new DbHelper(this); //modified
        sqLiteDatabase = dbHelper.getWritableDatabase();
        dbHelper.addInformation(darek, sqLiteDatabase);
        Toast.makeText(getBaseContext(), "Tadáá", Toast.LENGTH_LONG).show();
        dbHelper.close();
    } 
}