我不知道为什么,但是我从CreationCompte.java中插入的内容不再在我的sqlite数据库中注册,我没有错误消息,并且在logcat文件中调用了insertUser函数,我不了解正在发生的情况android的新手,有人可以帮助我找到问题吗?
ps:我的XML文件似乎很清晰。
MainActivity.java:
package com.example.tp1;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
private EditText Login;
private EditText password;
private Button submit;
private int counter = 5;
private TextView Info;
private Button inscription;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Login = (EditText)findViewById(R.id.Login);
password = (EditText)findViewById(R.id.password);
submit = (Button) findViewById(R.id.submit);
inscription = (Button) findViewById(R.id.inscription);
Info = (TextView) findViewById(R.id.Info);
Intent intent = getIntent();
if (intent != null) {
String login = "";
String mdp = "";
if (intent.hasExtra("login") && intent.hasExtra("mdp")) { // vérifie qu'une valeur est associée à la clé “login”
login = intent.getStringExtra("login"); // on récupère la valeur associée à la clé
mdp = intent.getStringExtra("mdp");
Login.setText(login);
password.setText(mdp);
}
}
Info.setText("No of attempts remaining : 5");
submit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
validate(Login.getText().toString(), password.getText().toString());
}
});
inscription.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
sinscrire();
}
});
}
private void validate(String Login, String password) {
if(Login.equals(getString(R.string.Login)) && password.equals(getString(R.string.password))) {
Intent intent = new Intent(MainActivity.this, SecondActivity.class);
intent.putExtra("Login", Login);
startActivity(intent);
} else {
counter--;
Info.setText("nb of attempts remaining" + String.valueOf(counter));
if (counter==0) {
submit.setEnabled(false);
}
}
}
private void sinscrire() {
Intent intent = new Intent(MainActivity.this, creationCompte.class);
startActivity(intent);
}
}
CreationCompte.java:
package com.example.tp1;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class creationCompte extends AppCompatActivity {
private EditText nom;
private EditText prenom;
private EditText telephone;
private EditText courriel;
private EditText login;
private EditText mdp;
private EditText confirmationmdp;
private Button valider;
private Button annuler;
private SQLiteDatabaseHelper DataBaseManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_creation_compte);
nom = (EditText)findViewById(R.id.nom);
prenom = (EditText)findViewById(R.id.prenom);
telephone = (EditText) findViewById(R.id.telephone);
courriel = (EditText) findViewById(R.id.courriel);
login = (EditText)findViewById(R.id.login);
mdp = (EditText)findViewById(R.id.mdp);
confirmationmdp = (EditText)findViewById(R.id.confirmationmdp);
valider = (Button) findViewById(R.id.valider);
annuler = (Button) findViewById(R.id.annuler);
valider.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
validation(nom, prenom, telephone, courriel, login, mdp, confirmationmdp);
}
});
annuler.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
annulation();
}
});
}
private void validation(EditText nom, EditText prenom, EditText telephone, EditText courriel, EditText login, EditText mdp, EditText confirmationmdp ) {
if (mdp.getText().toString().equals(confirmationmdp.getText().toString())) {
DataBaseManager = new SQLiteDatabaseHelper(this);
DataBaseManager.insertUser(nom.getText().toString(), prenom.getText().toString(), telephone.getText().toString(), courriel.getText().toString(), login.getText().toString(), mdp.getText().toString());
DataBaseManager.close();
Intent intent = new Intent(creationCompte.this, MainActivity.class);
intent.putExtra("login", login.getText().toString());
intent.putExtra("mdp", mdp.getText().toString());
startActivity(intent);
} else {
Toast.makeText(getApplicationContext(),"nous n'avons pas compris votre mot de passe !", Toast.LENGTH_LONG).show();
}
}
private void annulation() {
Intent intent = new Intent(creationCompte.this, MainActivity.class);
startActivity(intent);
}
}
SQLiteDataBaseHelper.java:
package com.example.tp1;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
public class SQLiteDatabaseHelper extends SQLiteOpenHelper {
private static final String DATABASE_NAME = "nt3.db";
private static final int DATABASE_VERSION = 1;
private static final String TABLE_NAME = "user_table";
public SQLiteDatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
String strSql = "create table user_table ("
+ " idUser integer primary key autoincrement,"
+ " nom text not null,"
+ " prenom text not null,"
+ " telephone text not null,"
+ " courriel text not null,"
+ " login text not null,"
+ " mdp text not null"
+ ")";
db.execSQL(strSql);
Log.i( "DATABASE", "onCreate invoked" );
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
String strSql = "drop table user_table";
db.execSQL(strSql);
Log.i( "DATABASE", "onUpgrade invoked" );
}
public void insertUser(String nom, String prenom, String telephone, String courriel, String login, String password) {
nom = nom.replace("'", "''");
prenom = prenom.replace("'", "''");
telephone = telephone.replace("'", "''");
courriel = courriel.replace("'", "''");
login = login.replace("'", "''");
password = password.replace("'", "''");
String strSql = "insert into user_table (nom, prenom, telephone, courriel, login, mdp) values ('" + nom + "', '" + prenom + "', '" + telephone + "', '" + courriel + "', '" + login + "', '" + password + "')";
this.getWritableDatabase().execSQL(strSql);
Log.i( "DATABASE", "insertUser invoked" );
}
}
答案 0 :(得分:0)
问题不是不是没有插入数据,而是您用来得出结论的任何方法都是错误的。
通过将 insertuser 方法修改为:-
public void insertUser(String nom, String prenom, String telephone, String courriel, String login, String password) {
Log.i( "DATABASE", "insertUser invoked" ); //<<<<<<<<<< MOVED AS MORE APPROPRIATE HERE
nom = nom.replace("'", "''");
prenom = prenom.replace("'", "''");
telephone = telephone.replace("'", "''");
courriel = courriel.replace("'", "''");
login = login.replace("'", "''");
password = password.replace("'", "''");
String strSql = "insert into user_table (nom, prenom, telephone, courriel, login, mdp) values ('" + nom + "', '" + prenom + "', '" + telephone + "', '" + courriel + "', '" + login + "', '" + password + "')";
this.getWritableDatabase().execSQL(strSql);
DatabaseUtils.dumpCursor(this.getWritableDatabase().query("user_table",null,null,null,null,null,null)); //<<<<<<<<<< ADDDED to check the data
}
然后得到(示例):-
04-11 09:25:51.549 10907-10907/aaa.so55617018userIssue I/DATABASE: insertUser invoked
04-11 09:25:51.570 10907-10907/aaa.so55617018userIssue I/DATABASE: onCreate invoked
04-11 09:25:51.577 10907-10907/aaa.so55617018userIssue I/System.out: >>>>> Dumping cursor android.database.sqlite.SQLiteCursor@3aabb7c1
04-11 09:25:51.577 10907-10907/aaa.so55617018userIssue I/System.out: 0 {
04-11 09:25:51.577 10907-10907/aaa.so55617018userIssue I/System.out: idUser=1
04-11 09:25:51.577 10907-10907/aaa.so55617018userIssue I/System.out: nom=Fred
04-11 09:25:51.577 10907-10907/aaa.so55617018userIssue I/System.out: prenom=Bloggs
04-11 09:25:51.577 10907-10907/aaa.so55617018userIssue I/System.out: telephone=1234567890
04-11 09:25:51.577 10907-10907/aaa.so55617018userIssue I/System.out: courriel=fred@bloggsmail.com
04-11 09:25:51.578 10907-10907/aaa.so55617018userIssue I/System.out: login=fred
04-11 09:25:51.578 10907-10907/aaa.so55617018userIssue I/System.out: mdp=fred
04-11 09:25:51.578 10907-10907/aaa.so55617018userIssue I/System.out: }
04-11 09:25:51.578 10907-10907/aaa.so55617018userIssue I/System.out: <<<<<
相反,您的问题似乎是,当用户输入数据后尝试验证登录名时,您正在对照从资源字符串而非数据库获得的值来检查登录名/密码。按照:-
if(Login.equals(getString(R.string.Login)) && password.equals(getString(R.string.password))) {
相反,您希望基于以下内容:-
+ " login text UNIQUE not null,"
来确保登录为UNQIUE(以及一些检查以防止已使用的登录被使用)。例如:-
public String getPasswordForUser(String login) {
String rv = "";
SQLiteDatabase db = this.getWritableDatabase();
Cursor csr = db.query("user_table",null,"login=?",new String[]{login},null,null,null);
if (csr.moveToFirst()) {
rv = csr.getString(csr.getColumnIndex("mdp"));
}
csr.close();
return rv;
}
请注意,如果复制了登录名,则上面只会考虑首次登录(请参见上文,将用户名更改为UNIQUE)。
请注意,如果在登录列的定义中对inlcude UNIQUE进行了更改,则需要执行以下操作之一,然后重新运行该应用程序:-
然后
:-
private void validate(String Login, String password) {
SQLiteDatabaseHelper dbhlpr = new SQLiteDatabaseHelper(this);
String stored_password = dbhlpr.getPasswordForUser(Login); //<<<<<<<<<< not really needed see below
if (password.equals(dbhlpr.getPasswordForUser(Login))) {
//if(Login.equals(getString(R.string.Login)) && password.equals(getString(R.string.password))) {
//Intent intent = new Intent(MainActivity.this, SecondActivity.class);
//intent.putExtra("Login", Login);
//startActivity(intent);
Info.setText("Logged In"); //<<<<<<<<<< ADDED
} else {
counter--;
Info.setText("nb of attempts remaining" + String.valueOf(counter));
if (counter==0) {
submit.setEnabled(false);
}
}
}
if (password.equals(dbhlpr.getPasswordForUser(Login))) {
那样进行检查尝试启动 MainActivity 作为尝试,从 creationCompte 活动返回数据并不是真正的正确方法。推荐的方法是启动 creationCompte 以获得结果,设置返回Intent,并完成 creationCompte ,它将根据Getting a Result from an Activity返回MainActivity。
答案 1 :(得分:0)
您好,MikeT非常感谢您的解释和建议。
我终于找到了解决我问题的方法,答案是实际上没有任何问题。 实际上,当我提取数据库时,我将其提取到与以前相同的文件夹中,因此android studio问我是否要替换它,并且天真地我单击了yes,但实际上根本没有替换它。 因此,要查看数据库中的新插入内容,我必须将其提取到新文件夹中,以避免出现替换问题。然后我建立了一个新的连接,我所有的新数据都出现了! 就这么简单,但我的搜索方向错误。
最诚挚的问候,
新手