我是android app developpement的新手。我试图通过下面的代码将值插入SQLite数据库;它不起作用。我绝望了! 任何人都可以帮助我吗?
这是DatabaseHelper.JAVA
public class DatabaseHelper extends SQLiteOpenHelper {
private static final int DATABASE_VERSION = 1;
private static final String DATABASE_NAME = "agents.db";
private static final String TABLE_NAME = "agents";
private static final String COLUMN_ID = "id";
private static final String COLUMN_MATRICULE = "matricule";
private static final String COLUMN_NOM = "nom";
private static final String COLUMN_PRENOM = "prenom";
private static final String COLUMN_FONCTION = "fonction";
private static final String COLUMN_EMAIL = "email";
private static final String COLUMN_PASS = "pass";
private static final String COLUMN_ADDRESS = "address";
private static final String COLUMN_CIN = "cin";
SQLiteDatabase db;
private static final String TABLE_CREATE = "create table agents (id integer primary key not null , " +
", matricule integer not null , nom text not null , prenom text not null , fonction text not null , email text not null , pass text not null , address text not null);";
public DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, 1);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(TABLE_CREATE);
this.db = db;
}
public void insertAgent(Agent a) {
db = this.getWritableDatabase();
ContentValues values = new ContentValues();
String query = "select * from agents";
Cursor cursor = db.rawQuery(query , null);
int count = cursor.getCount();
values.put(COLUMN_ID , count);
values.put(COLUMN_MATRICULE, a.getMatricule());
values.put(COLUMN_NOM, a.getNom());
values.put(COLUMN_PRENOM, a.getPrenom());
values.put(COLUMN_FONCTION, a.getFonction());
values.put(COLUMN_EMAIL, a.getEmail());
values.put(COLUMN_PASS, a.getPass());
values.put(COLUMN_ADDRESS, a.getAddress());
values.put(COLUMN_CIN, a.getCin());
db.insert(TABLE_NAME, null, values);
db.close();
}
public String searchPass(String matricule)
{
db = this.getReadableDatabase();
String query = "select matricule, pass from "+TABLE_NAME;
Cursor cursor = db.rawQuery(query , null);
String a, b;
b = "not found";
if(cursor.moveToFirst())
{
do{
a = cursor.getString(0);
if(a.equals(matricule))
{
b = cursor.getString(1);
break;
}
}
while(cursor.moveToNext());
}
return b;
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
String query = "DROP TABLE IF EXISTS "+TABLE_NAME;
db.execSQL(query);
this.onCreate(db);
}
}
Agent.JAVA
public class Agent {
String matricule,nom,prenom,fonction,email,pass,address,cin;
public String getMatricule() {
return matricule;
}
public void setMatricule(String matricule) {
this.matricule = matricule;
}
public String getNom() {
return nom;
}
public void setNom(String nom) {
this.nom = nom;
}
public String getPrenom() {
return prenom;
}
public void setPrenom(String prenom) {
this.prenom = prenom;
}
public String getFonction() {
return fonction;
}
public void setFonction(String fonction) {
this.fonction = fonction;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getPass() {
return pass;
}
public void setPass(String pass) {
this.pass = pass;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getCin() {
return cin;
}
public void setCin(String cin) {
this.cin = cin;
}
}
InscRire.JAVA
public class InscRire extends Activity {
DatabaseHelper helper = new DatabaseHelper(this);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.sinscrire);
}
public void onSignUpClick(View v)
{
if(v.getId()== R.id.Bsinscrire)
{
EditText matricule = (EditText)findViewById(R.id.TFmatricule);
EditText nom = (EditText)findViewById(R.id.TFnom);
EditText prenom = (EditText)findViewById(R.id.TFprenom);
EditText fonction = (EditText)findViewById(R.id.TFfonction);
EditText email = (EditText)findViewById(R.id.TFemail);
EditText pass1 = (EditText)findViewById(R.id.TFpass1);
EditText pass2 = (EditText)findViewById(R.id.TFpass2);
EditText address = (EditText)findViewById(R.id.TFaddress);
EditText cin = (EditText)findViewById(R.id.TFcin);
String matriculestr = matricule.getText().toString();
String nomstr = nom.getText().toString();
String prenomstr = prenom.getText().toString();
String fonctionstr = fonction.getText().toString();
String emailstr = email.getText().toString();
String pass1str = pass1.getText().toString();
String pass2str = pass2.getText().toString();
String addressstr = address.getText().toString();
String cinstr = cin.getText().toString();
if(!pass1str.equals(pass2str))
{
//popup msg
Toast pass = Toast.makeText(InscRire.this , "Mot de passe incorrect!" , Toast.LENGTH_SHORT);
pass.show();
}
else
{
//insert the detailes in database
Agent a = new Agent();
a.setMatricule(matriculestr);
a.setNom(nomstr);
a.setPrenom(prenomstr);
a.setFonction(fonctionstr);
a.setEmail(emailstr);
a.setPass(pass1str);
a.setAddress(addressstr);
a.setCin(cinstr);
helper.insertAgent(a);
}
}
}
}
Main.JAVA
public class MainActivity extends ActionBarActivity {
DatabaseHelper helper = new DatabaseHelper(this);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
public void onButtonClick(View v)
{
if(v.getId() == R.id.Bconnexion)
{
EditText a = (EditText)findViewById(R.id.TFmatricule);
String mat = a.getText().toString();
EditText b = (EditText)findViewById(R.id.TFpass);
String pass = b.getText().toString();
String mot_de_pass = helper.searchPass(mat);
if(pass.equals(mot_de_pass))
{
Intent i = new Intent(MainActivity.this, Display.class);
i.putExtra("Matricule",mat);
startActivity(i);
}
else
{
Toast temp = Toast.makeText(MainActivity.this , "Matricule et mot de passe incorrect!" , Toast.LENGTH_SHORT);
temp.show();
}
}
if(v.getId() == R.id.Binscrire)
{
Intent i = new Intent(MainActivity.this, InscRire.class);
startActivity(i);
}
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
Display.JAVA
public class Display extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.display);
String matricule = getIntent().getStringExtra("Matricule");
TextView tv = (TextView)findViewById(R.id.TVmatricule);
tv.setText(matricule);
}
}
sinscrire.XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent"
android:orientation="vertical">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Matricule "
android:id="@+id/textView1" />
<EditText
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/TFmatricule"
android:inputType="number"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Nom"
android:id="@+id/textView2" />
<EditText
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/TFnom" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Prenom"
android:id="@+id/textView3" />
<EditText
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/TFprenom" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Fonction"
android:id="@+id/textView4" />
<EditText
android:layout_width="match_parent"
android:layout_height="match_parent"
android:ems="10"
android:id="@+id/TFfonction" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Email"
android:id="@+id/textView5" />
<EditText
android:layout_width="match_parent"
android:layout_height="match_parent"
android:inputType="textEmailAddress"
android:ems="10"
android:id="@+id/TFemail" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Mot de passe"
android:id="@+id/textView6" />
<EditText
android:layout_width="match_parent"
android:layout_height="match_parent"
android:inputType="textPassword"
android:ems="10"
android:id="@+id/TFpass1" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Confirmer mot de passe"
android:id="@+id/textView7" />
<EditText
android:layout_width="match_parent"
android:layout_height="match_parent"
android:inputType="textPassword"
android:ems="10"
android:id="@+id/TFpass2" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Address"
android:id="@+id/textView8" />
<EditText
android:layout_width="match_parent"
android:layout_height="match_parent"
android:ems="10"
android:id="@+id/TFaddress" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Cin"
android:id="@+id/textView9" />
<EditText
android:layout_width="match_parent"
android:layout_height="match_parent"
android:inputType="number"
android:ems="10"
android:id="@+id/TFcin" />
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Sinscrire"
android:id="@+id/Bsinscrire"
android:onClick="onSignUpClick" />
</LinearLayout>
</ScrollView>
</LinearLayout>
main.xml中
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Connexion"
android:id="@+id/Bconnexion"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:onClick="onButtonClick" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Matricule"
android:id="@+id/textView2"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/TFmatricule"
android:layout_below="@+id/textView2"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Mot de passe"
android:id="@+id/textView3"
android:layout_below="@+id/TFmatricule"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:inputType="textPassword"
android:ems="10"
android:id="@+id/TFpass"
android:layout_below="@+id/textView3"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="S'inscrire ici"
android:id="@+id/Binscrire"
android:layout_below="@+id/Bconnexion"
android:layout_toRightOf="@+id/textView3"
android:layout_toEndOf="@+id/textView3"
android:onClick="onButtonClick" />
</RelativeLayout>
Display.XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent"
android:orientation="vertical"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Welcome,"
android:id="@+id/textView" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="@string/emptyString"
android:id="@+id/TVmatricule" />
</LinearLayout>