感谢Midhun MP,这两个问题都解决了!我有一个新问题:我有 2 个字符串来接管数据,一个来 self 输入的数据,一个来自数据库,我使用函数 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 程序快把我逼疯了。
这有点像登录程序,我有两个 Activity :第一个是让用户输入密码,我认为它需要获取用户输入的内容并与数据库中的数据进行比较; 第二个是给用户修改密码的,我觉得就是更新数据库中的数据
我已经使用 sqlite 为代码建立了一个数据库。(现在只有一个代码只有一列,但可能会有不同的用户)
这是我的问题:
1.根据我的模拟器,我可以向数据库中插入数据,但无法更新它。这很奇怪,因为当我单击更新按钮时,它告诉我代码已更改。
2. 我需要我的第一个 Activity 来获取代码(保存在第二个 Activity 创建的数据库中),并与用户输入的内容进行比较。但是我不知道该怎么做...
我的数据库代码:
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;
}
}
和我的第二个 Activity 的代码(无法更新数据):
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();
}
}
}
}
);
}
}
对于第一个 Activity ,我使用 Cursor 传递数据
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
第一行是从数据库中获取的代码。有用!只是不知何故,它与我输入的内容不匹配。哦啦啦....
最佳答案
问题是由于以下行而发生的:
db.update(TABLE_NAME, contentValues, "code = ?", new String[]{ CODE });
假设您的数据库包含一个值“Elsa”,现在您正尝试将其更新为“Hello”,上面的 sql 查询代码看起来类似于:
UPDATE Code_table SET CODE = 'Hello' WHERE code = 'Hello'
上面的 where 子句将为 false,该行将不会更新。
你需要使用:
db.update(TABLE_NAME, contentValues, null, null);
第三个和第四个参数代表WhereClause
和WhereArgs
。在您的情况下,您不需要指定这些(因为您没有定义任何唯一或主键字段)。
现在查询将如下所示:
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();
}
关于android - 如何从另一个 Activity 创建的数据库中获取数据/无法将数据更新到数据库中(Android studio,SQLite),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37598859/