简体   繁体   English

如何在Android应用程序中使用SQLite?

[英]How can I use SQLite in android application?

How can I ensure that my tables are created and where can I find my database in android? 如何确保创建了表,以及如何在android中找到数据库? I ran this code but it stores only one record in my tables. 我运行了这段代码,但它仅在我的表中存储一条记录。 How can this problem be solved? 如何解决这个问题? I have created two classes 我创建了两个类

FIRST CLASS: 头等舱:

 public class dataAccess {
     private SQLiteDatabase database;
     private sqliteHelper dbHelper;

     public dataAccess(Context context) 
     {
          dbHelper = new sqliteHelper(context);
     }

     public void open() throws SQLException 
     {
       database = dbHelper.getWritableDatabase();
     } 

     public void close() 
     {
        dbHelper.close();
     }

     public void createDoctorInfo(String FirstName ,String LastName) 
     {
        ContentValues values = new ContentValues();
        values.put(sqliteHelper.FirstName, FirstName);
        values.put(sqliteHelper.LastName, LastName);

        database.insert(sqliteHelper.TABLE_DoctorInfo, null, values);
     }


}

SECOND CLASS: 第二类:

public class sqliteHelper extends SQLiteOpenHelper
{
      public static final String TABLE_DoctorInfo = "DoctorInfo";
      public static final String DoctorId = "_id";
      public static final String FirstName = "FirstName";
      public static final String LastName = "LastName";


      private static final String DATABASE_NAME = "Medical.db";
      private static final int DATABASE_VERSION = 1;

      // Database creation sql statement
      private static final String DATABASE_CREATE = " CREATE TABLE "
      + TABLE_DoctorInfo + "("+DoctorId + " INTEGER PRIMARY KEY AUTOINCREMENT, "
 + FirstName + " TEXT NOT NULL ," +LastName +" TEXT NOT NULL);" ;

      public sqliteHelper(Context context)
      {
          super(context, DATABASE_NAME, null, DATABASE_VERSION);
      }


    @Override
     public void onCreate(SQLiteDatabase database)
    {
        database.execSQL(DATABASE_CREATE);  

    }

    @Override
    public void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2)
    {


}

MAIN ACTIVITY: 主要活动:

public class MainActivity extends Activity {

    EditText  fname,lname;
    Button confirm,view;
    dataAccess da=new dataAccess(this);

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        da.open();
        fname=(EditText)findViewById(R.id.fname);
        lname=(EditText)findViewById(R.id.lname);
        confirm=(Button)findViewById(R.id.confirm);
        view=(Button)findViewById(R.id.view);

         confirm.setOnClickListener(new View.OnClickListener() {


            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                saveDoctorInfo();
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
    public void saveDoctorInfo(){

      String fn=fname.getText().toString();
       String ln=lname.getText().toString();


       //save Info into DB
      da.createDoctorInfo(fn,ln);
      //close Conection 
      da.close();
      Toast.makeText(getBaseContext(), "Data Has been saved successfully", Toast.LENGTH_LONG).show();

    }

}

And this is my xml file 这是我的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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <TextView
        android:id="@+id/fnamet"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="41dp"
        android:layout_marginTop="35dp"
        android:text="@string/first_name" />

    <EditText
        android:id="@+id/fname"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/fnamet"
        android:layout_marginLeft="36dp"
        android:layout_marginTop="16dp"
        android:ems="10"
        android:inputType="textPersonName" >

        <requestFocus />
    </EditText>

    <TextView
        android:id="@+id/lnamet"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/fname"
        android:layout_below="@+id/fname"
        android:layout_marginTop="30dp"
        android:text="@string/last_name" />

    <Button
        android:id="@+id/confirm"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/lnamet"
        android:layout_below="@+id/lname"
        android:layout_marginLeft="24dp"
        android:layout_marginTop="50dp"
        android:text="@string/confirm" />

    <Button
        android:id="@+id/view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/confirm"
        android:layout_below="@+id/confirm"
        android:layout_marginTop="36dp"
        android:text="@string/view" />

    <EditText
        android:id="@+id/lname"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/lnamet"
        android:layout_below="@+id/lnamet"
        android:layout_marginTop="18dp"
        android:ems="10"
        android:inputType="textPersonName" />


</RelativeLayout>

how can ensure that my tables are created 如何确保创建我的表

By using the convenient open helper. 通过使用方便的开放式助手。 It helps you open a connection to a fully initialized database ( getWritableDatabase ). 它可以帮助您打开与完全初始化的数据库( getWritableDatabase )的连接。 That includes creating the database from scratch and upgrading the database schema if you need to. 这包括从头开始创建数据库,并在需要时升级数据库模式。 The only thing you need to do is to specify what SQL command need to be run in onCreate or onUpgrade . 您唯一需要做的就是指定要在onCreateonUpgrade运行的SQL命令。

where can i find my database in android 我在哪里可以找到我在android中的数据库

You should not need to care about the exact path. 您无需关心确切的路径。 It's in your app private data directory. 它在您的应用程序私有数据目录中。 DATABASE_NAME = "Medical.db" will - on a typical device - result in DATABASE_NAME = "Medical.db" -在典型设备上-将导致
/data/data/your.package.name/databases/Medical.db . /data/data/your.package.name/databases/Medical.db

it stores only one record in my tables how can solve such problem 它只在我的表中存储一条记录如何解决此类问题

Change the onCreate part. 更改onCreate部分。 But note that once the database is created the open helper will not try to recreate it. 但是请注意,一旦创建了数据库,打开的帮助程序将不会尝试重新创建它。 You either need to wipe the app data or need to tell open helper that the version has changed. 您要么需要擦除应用程序数据,要么需要告诉Open Helper版本已更改。 It will then call onUpgrade which you can use to delete old table definitions / alter them / ... 然后它将调用onUpgrade ,您可以使用它删除旧表定义/更改它们/ ...

try to refer the answer in this question, this help a lot in creating database and insert value. 尝试参考该问题的答案,这对创建数据库和插入值有很大帮助。 LINK 链接

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM