Ajouter des données Table en utilisant SQLite sous Android sutdio | Exam-Lib
  1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Ajouter des données Table en utilisant SQLite sous Android sutdio

abdelouafiMar 26, 2018

    1. abdelouafi

      abdelouafi Administrator Staff Member

      Messages:
      636
      Likes Received:
      14
      Trophy Points:
      18
      Joined
      Sep 13, 2016

      On va essayer de réaliser cette interface:
      upload_2018-3-26_23-6-47.png

      LE code source du "activity_main.xml":


      Code:
      <?xml version="1.0" encoding="utf-8"?>
      <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          xmlns:app="http://schemas.android.com/apk/res-auto"
          xmlns:tools="http://schemas.android.com/tools"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:orientation="vertical"
          tools:context="test1.ista.main.bdd.MainActivity">
      
          <TextView
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:text="Login:"
              android:textSize="18sp"
              app:layout_constraintBottom_toBottomOf="parent"
              app:layout_constraintLeft_toLeftOf="parent"
              app:layout_constraintRight_toRightOf="parent"
              android:layout_alignParentTop="true"
              android:layout_marginTop="12dp"
              app:layout_constraintTop_toTopOf="parent"
              android:layout_alignParentLeft="true"
              android:layout_alignParentStart="true"
              android:gravity="center"
              android:id="@+id/textView"
              />
          <EditText
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:id="@+id/editlogin"
              android:textStyle="bold|italic"
              android:hint="Donnez votre login"
              android:layout_below="@+id/textView"
      
              />
      
          <TextView
              android:layout_below="@+id/editlogin"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:text="Mot de passe:"
              android:textSize="18sp"
              app:layout_constraintBottom_toBottomOf="parent"
              app:layout_constraintLeft_toLeftOf="parent"
              app:layout_constraintRight_toRightOf="parent"
              app:layout_constraintTop_toTopOf="parent"
              android:id="@+id/pwd"/>
          <EditText
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:id="@+id/editpwd"
              android:textStyle="bold|italic"
              android:hint="Donnez votre mot de passe"
              android:layout_below="@+id/pwd"
              />
      
          <Button
      
              android:layout_below="@+id/pwd"
              android:gravity="right"
              android:textColor="#f00"
              android:textSize="25sp"
              android:text="add_user"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:id="@+id/button"
              android:textStyle="bold|italic"
              android:background="#147D03"
              android:onClick="addUser"
              android:layout_marginLeft="28dp"
              android:layout_marginStart="28dp"
      
              android:layout_alignParentLeft="true"
              android:layout_alignParentStart="true"
              android:layout_marginTop="23dp" />
      
      
          <Button
              android:text="Afficher"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:id="@+id/button2"
              android:textSize="18sp"
              android:onClick="viewdata"
              android:textStyle="bold|italic"
              android:layout_alignBaseline="@+id/button"
              android:layout_alignBottom="@+id/button"
              android:layout_alignRight="@+id/button4"
              android:layout_alignEnd="@+id/button4" />
      
      </LinearLayout>
      


      Le code source de la page "MainActivity.java":

      Code:
      package test1.ista.main.bdd;
      
      import android.content.ContentValues;
      import android.content.Context;
      import android.database.Cursor;
      import android.database.sqlite.SQLiteDatabase;
      import android.database.sqlite.SQLiteOpenHelper;
      import android.support.v7.app.AppCompatActivity;
      import android.os.Bundle;
      import android.view.View;
      import android.widget.EditText;
      import android.widget.Toast;
      
      public class MainActivity extends AppCompatActivity {
           EditText  Name, Pass, updateold, updatenew, delete;
          myDbAdapter helper;
      
      
          @Override
          protected void onCreate(Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);
              setContentView(R.layout.activity_main);
              Name = (EditText) findViewById(R.id.editlogin);
              Pass = (EditText) findViewById(R.id.editpwd);
              helper = new myDbAdapter(this);
          }
      
      // ajouter un utilisateur
      
          public void addUser(View view)
          {
              String t1 = Name.getText().toString();
              String t2 = Pass.getText().toString();
              if(t1.isEmpty() || t2.isEmpty())
              {
                  Message.message(getApplicationContext(),"Enter Both Name and Password");
              }
              else
              {
                  long id = helper.insertData(t1,t2);
                  if(id<=0)
                  {
                      Message.message(getApplicationContext(),"Insertion Unsuccessful");
                      Name.setText("");
                      Pass.setText("");
                  } else
                  {
                      Message.message(getApplicationContext(),"Insertion Successful");
                      Name.setText("");
                      Pass.setText("");
                  }
              }
          }
          //fin de l'ajout d'un nouveau utilisateur
          public void viewdata(View view)
          {
              String data = helper.getData();
              Message.message(this,data);
          }
      
      
      
          public class myDbAdapter {
              myDbHelper myhelper;
      
              public myDbAdapter(Context context) {
                  myhelper = new myDbHelper(context);
              }
      
              //insertion des données:
              public long insertData(String name, String pass)
              {
                  SQLiteDatabase dbb = myhelper.getWritableDatabase();
                  ContentValues contentValues = new ContentValues();
                  contentValues.put(myDbHelper.NAME, name);
                  contentValues.put(myDbHelper.MyPASSWORD, pass);
                  long id = dbb.insert(myDbHelper.TABLE_NAME, null , contentValues);
                  return id;
              }
      
      
              //affichage
              public String getData()
              {
                  SQLiteDatabase db = myhelper.getWritableDatabase();
                  String[] columns = {myDbHelper.UID,myDbHelper.NAME,myDbHelper.MyPASSWORD};
                  Cursor cursor =db.query(myDbHelper.TABLE_NAME,columns,null,null,null,null,null);
                  StringBuffer buffer= new StringBuffer();
                  while (cursor.moveToNext())
                  {
                      int cid =cursor.getInt(cursor.getColumnIndex(myDbHelper.UID));
                      String name =cursor.getString(cursor.getColumnIndex(myDbHelper.NAME));
                      String  password =cursor.getString(cursor.getColumnIndex(myDbHelper.MyPASSWORD));
                      buffer.append(cid+ "   " + name + "   " + password +" \n");
                  }
                  return buffer.toString();
              }
      
      
          }
      
      
      
          //classe Message
          public static class Message {
              public static void message(Context context, String message) {
                  Toast.makeText(context, message, Toast.LENGTH_LONG).show();
              }
          }
      
      
      
          static class myDbHelper extends SQLiteOpenHelper
          {
              private static final String DATABASE_NAME = "myDatabase";    // Database Name
              private static final String TABLE_NAME = "myTable";   // Table Name
              private static final int DATABASE_Version = 1;    // Database Version
              private static final String UID="_id";     // Column I (Primary Key)
              private static final String NAME = "Name";    //Column II
              private static final String MyPASSWORD= "Password";    // Column III
              private static final String CREATE_TABLE = "CREATE TABLE "+TABLE_NAME+
                      " ("+UID+" INTEGER PRIMARY KEY AUTOINCREMENT, "+NAME+" VARCHAR(255) ,"+ MyPASSWORD+" VARCHAR(225));";
              private static final String DROP_TABLE ="DROP TABLE IF EXISTS "+TABLE_NAME;
              private Context context;
      
              public myDbHelper(Context context) {
                  super(context, DATABASE_NAME, null, DATABASE_Version);
                  this.context=context;
              }
      
      
              //création de la base de données avec la table "personne"
              public void onCreate(SQLiteDatabase db) {
      
                  try {
                      db.execSQL(CREATE_TABLE);
                  } catch (Exception e) {
                      Message.message(context,""+e);
                  }
              }
              //fin creation de la table
      
      
              public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
                  try {
                      Message.message(context,"OnUpgrade");
                      db.execSQL(DROP_TABLE);
                      onCreate(db);
                  }catch (Exception e) {
                      Message.message(context,""+e);
                  }
              }
          }
      
      
      
      
      }
      
       
      Last edited: Mar 26, 2018
      Loading...

Share This Page

Share