【问题标题】:How to get the data from the database created by another activity/ Fails to update data into database (Android studio,SQLite)如何从另一个活动创建的数据库中获取数据/无法将数据更新到数据库中(Android Studio,SQLite)
【发布时间】:2016-10-02 14:29:49
【问题描述】:

感谢Midhun MP,这两个问题都解决了!我有一个新问题:我有 2 个字符串来接管数据,一个来自我输入的内容,一个来自数据库,我使用函数 equals() 进行比较,但它们不匹配,即使它们是对我来说相同的数字...我在下面更新:

public void buttonOpen(View view) {
    EditText txtpass1 = (EditText) findViewById(R.id.password);  //on recuperer le mot de passeword
    String pass1str = txtpass1.getText().toString();

    Toast.makeText(ParCode.this, pass1str, Toast.LENGTH_LONG).show();//test

    Cursor allCode = new DataCode(this).getCode();//get the data from database using Cursor
    allCode.moveToFirst();
    while (!allCode.isAfterLast())
    {
        String code = allCode.getString(0);
        Log.i(code, "Code");
        allCode.moveToNext();
    }
    String pass2str = allCode.toString();//le data dans le bibliotheque

    if (pass1str.equals(pass2str)) {...}

是数据类型的问题吗?


大家好。我是新手,我的 android-studio 程序让我发疯。
有点像登录程序,我有两个活动:第一个是让用户输入密码,我认为它需要获取用户输入的内容并与数据库中的数据进行比较; 第二个是用户修改密码,我认为这意味着更新数据库中的数据
我已经使用 sqlite 为代码构建了一个数据库。(现在只有一个代码只有一列,但进一步可能会有不同的用户)

这是我的问题:
1.根据我的模拟器,我可以将数据插入数据库,但无法更新。这很奇怪,因为当我单击更新按钮时,它告诉我代码已更改。
2. 我需要我的第一个活动来获取代码(保存在第二个活动创建的数据库中),并与用户输入的内容进行比较。但我不知道该怎么做...
我的数据库代码:
public class DataCode extends SQLiteOpenHelper {
public static final String DATABASE_NAME = "Code4.dp";
public static final String TABLE_NAME = "Code_table";
public static final String COL_1 = "CODE";//a une seule ligne

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

@Override
public void onCreate(SQLiteDatabase db) {
    db.execSQL("create table " + TABLE_NAME + " (CODE TEXT )");//create the table
}

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

//insert data
public boolean insertData(String CODE) {
    SQLiteDatabase db = this.getWritableDatabase();//insert to database
    ContentValues contentValues = new ContentValues();
    contentValues.put(COL_1, CODE);

    long result = db.insert(TABLE_NAME, null, contentValues);
    if (result == -1)
        return false;
    else
        return true;
}

//show data
public Cursor getCode(){
    SQLiteDatabase db = this.getReadableDatabase();
    Cursor res = db.rawQuery("select * from " + TABLE_NAME, null);//store table in cursor
    return res;
}

//modifier code
public boolean updateCode(String CODE) {
    SQLiteDatabase db = this.getWritableDatabase();//create database
    ContentValues contentValues = new ContentValues();
    contentValues.put(COL_1, CODE);

    db.update(TABLE_NAME, contentValues, "code = ?", new String[]{ CODE });
    return true;
}
}

以及我的第二个活动的代码(无法更新数据):

public class ModifierC extends AppCompatActivity {
DataCode myDbC;//creat the database

EditText editCode;
Button Modifier;

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

    myDbC = new DataCode(this);//my database for code

    editCode = (EditText)findViewById(R.id.password2);
    Modifier = (Button)findViewById(R.id.buttonMP);
    buttonMP();//modifier le code

}

public void buttonMP(){//button to change the password

    Modifier.setOnClickListener(
            new View.OnClickListener() {

                public void onClick(View v) {

                    EditText txtpass2 = (EditText) findViewById(R.id.password2);

                    String pass2str = txtpass2.getText().toString();


                        Cursor res = myDbC.getCode();
                        if (res.getCount() == 0) {//if there is no data in the database, use the insert method
                            boolean isInserted = myDbC.insertData(editCode.getText().toString());
                            if (isInserted == true)
                                Toast.makeText(ModifierC.this, "Code enregistré", Toast.LENGTH_LONG).show();
                            else
                                Toast.makeText(ModifierC.this, "Pas encore de code", Toast.LENGTH_LONG).show();

                        }
                        else {//if there is already a code, use the update method
                            boolean isUpdated = myDbC.updateCode(editCode.getText().toString());//recuperer le code de password2
                            if (isUpdated == true)
                                Toast.makeText(ModifierC.this, "Code a changé", Toast.LENGTH_LONG).show();
                            else
                                Toast.makeText(ModifierC.this, "Error...", Toast.LENGTH_LONG).show();
                        }

                    }
                }

            }
    );
}
}

对于第一个活动,我使用光标传递数据

public class ParCode extends AppCompatActivity {
DataCode myC;

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

public void buttonOpen(View view) {
    EditText txtpass1 = (EditText) findViewById(R.id.password);  //get what user has entered
    String pass1str = txtpass1.getText().toString();          

    Cursor coderes = myC.getCode();// search code in the database
    String pass2str = coderes.getString(0);

但它不起作用,我想我可能会错过一些东西。


抱歉,这可能是一个太长且非常愚蠢的问题。但是我已经花了一个多星期的时间,所以我想我应该寻求帮助。这是我第一次构建一个android应用程序,我所有的问题都是因为缺乏基础知识。但是我真的要为我的考试做准备,现在没有时间坚持这个T T ...所以请给我一些建议!
多谢


logcat 给了我这个:
06-03 06:22:58.224 27801-27801/com.example.android.sacconnecte I/4444: Code
06-03 06:22:58.327 27801-27839/com.example.android.sacconnecte W/EGL_emulation: eglSurfaceAttrib not implemented
06-03 06:22:58.327 27801-27839/com.example.android.sacconnecte W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0xad79f0a0, error=EGL_SUCCESS
06-03 06:23:08.396 27801-27839/com.example.android.sacconnecte E/Surface: getSlotFromBufferLocked: unknown buffer: 0xab81b280

第一行是从数据库中获取的代码。有用!只是不知何故,它与我输入的内容不匹配。哦啦啦....

【问题讨论】:

    标签: android database sqlite android-studio android-sqlite


    【解决方案1】:

    由于以下行导致问题发生:

    db.update(TABLE_NAME, contentValues, "code = ?", new String[]{ CODE });
    

    假设您的 db 包含一个值“Elsa”,现在您正尝试将其更新为“Hello”,上面的 sql 查询代码看起来类似于:

    UPDATE Code_table SET CODE = 'Hello' WHERE code = 'Hello'
    

    上面的where子句为假,行不会被更新。

    你需要使用:

    db.update(TABLE_NAME, contentValues, null, null);
    

    第三个和第四个参数分别代表WhereClauseWhereArgs。在您的情况下,您不需要指定这些(因为您没有定义任何唯一或主键字段)。

    现在查询将如下所示:

    UPDATE Code_table SET CODE = 'Hello'
    

    使用游标获取结果,可以使用以下代码:

    Cursor allCode = new DataCode().getCode();
    
    allCode.moveToFirst();
    
    while (!allCode.isAfterLast())
    {
        String code = allCode.getString(0);
        Log.i(code, "Code");
        allCode.moveToNext();
    
    }
    

    【讨论】:

    • 那行得通!太感谢了!而且比喻真的很能帮助我理解~现在我只有一个问题要解决XD
    • 我还在纠结第二个问题,你也看一下好吗?我认为一旦创建了数据库,任何活动都可以使用光标来使用它,但它不会工作:(
    • 它不起作用...我无法在此处粘贴我的 logcat,我已在问题页面中对其进行了更新,您能帮我检查一下吗?
    • @Elsa:你能用allCode.getString(1);代替allCode.getString(0);吗?
    • 它显示“无法从具有 1 行 1 列的 CursorWindow 读取第 0 行第 1 列。”模拟器关闭。接着是“E/AndroidRuntime: FATAL EXCEPTION: main”和很多东西:o
    猜你喜欢
    • 2016-11-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-15
    • 2018-05-28
    • 2012-01-06
    相关资源
    最近更新 更多