jeudi 7 janvier 2016

Selecting Data from SQLite Database located in Assets folder

I am having problems with selecting the data from the table. Here is my codes:::

First: The test.sql File located in the Assets

BEGIN TRANSACTION;
CREATE TABLE `Student.db` (
    `NAME`  TEXT,
    `LAST`  TEXT,
    `WORK`  TEXT,
    `NATIONALITY`   TEXT,
    `SALARYINCHAR`  TEXT
);
INSERT INTO `Student.db` VALUES ('Besho','Jarrah','Teacher','Jordanian','Five Thousends');
INSERT INTO `Student.db` VALUES ('Khaled','Ahmad','Doctor','Syrian','Six Thousends');
COMMIT;

And this is my Code in the MyDataBase.Java Class:::

package com.besho.testtheassets;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;

import com.readystatesoftware.sqliteasset.SQLiteAssetHelper;

/**
 * Created by Besho on 1/7/2016.
 */
public class MyDatabase extends SQLiteAssetHelper {

    private static final String DATABASE_NAME = "test.sql";
    private static final int DATABASE_VERSION = 1;

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

}

And here is my MainActivity class code :::

package com.besho.testtheassets;

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    TextView txt;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        txt= (TextView) findViewById(R.id.txt);
        Button btn= (Button) findViewById(R.id.btn);



        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {


                String TABLE_NAME="Student.db";
                MyDatabase myDatabase = new MyDatabase(MainActivity.this);
                SQLiteDatabase db = myDatabase.getReadableDatabase();

                String selectQuery = "SELECT * FROM " + TABLE_NAME;
                Cursor cursor= db.rawQuery(selectQuery, null);
                if (cursor.moveToFirst()) {
                    StringBuffer buffer=new StringBuffer();
                    while (cursor.moveToNext())
                    {
                        buffer.append("Name: " + cursor.getString(0) + "\n");
                        buffer.append("Last: " + cursor.getString(1) + "\n");
                        buffer.append("Work " + cursor.getString(2) + "\n");
                        buffer.append("Nationality: " + cursor.getString(3) + "\n");
                        buffer.append("SalaryinChar: " + cursor.getString(4) + "\n\n\n");
                    }
                    txt.setText(buffer.toString());

                }

                }
        });
    }
}

The App stop working when i press the button to select the Data into the Text View. Please, Help me

Aucun commentaire:

Enregistrer un commentaire