mercredi 17 juin 2015

Android Database App Crash

I am new to Android And I need help to complete my app which stores or fetch value from SQLite database...

Here's my code for MainActivity.java

 package com.example.graphics;

import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Build;
import android.os.Bundle;
import android.os.StrictMode;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.example.graphics.db.SqlManager;

@TargetApi(Build.VERSION_CODES.GINGERBREAD) public class MainActivity extends Activity {




    Button bl,reg;
    EditText u,p;
    String x,y;


    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        StrictMode.ThreadPolicy policy=new StrictMode.ThreadPolicy.Builder().permitAll().build();
        StrictMode.setThreadPolicy(policy);

        bl=(Button) findViewById(R.id.bl);
        reg=(Button) findViewById(R.id.reg);
        u=(EditText) findViewById(R.id.u);
        p=(EditText) findViewById(R.id.p);
        x=u.getText().toString();
        y=p.getText().toString();




        reg.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {


                Toast.makeText(getApplicationContext(), "Database Updated...",
                           Toast.LENGTH_LONG).show();
                SqlManager db = new SqlManager(getApplicationContext());
                db.InsertNew(x,y);
                Intent intent=new Intent(v.getContext(), MainActivity.class);
                startActivityForResult(intent,1);
            }


        });




        bl.setOnClickListener(new View.OnClickListener() {


            public void onClick(View v) {


             {

                 SqlManager db = new SqlManager(getApplicationContext());
                 Cursor c = db.fetchData(); 
                    if(c.moveToFirst())
                    {
                        do
                        {
                            if(x==c.getString(0)) {
                                Intent intent=new Intent(v.getContext(), Login.class);
                                startActivityForResult(intent,1);
                            }

                        }while(c.moveToNext());
                    }
                    c.close();

            }

            }
        });




    }


    @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;
    }

}

Here's my SQLManager.java code :

    package com.example.graphics.db;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class SqlManager extends SQLiteOpenHelper {
    public static final String DB_NAME = "Graphics.db";
    public static final int SCHEMA_VERSION=1;
    static final String testTable = "test";

    public SqlManager(Context context) {
        super(context, DB_NAME, null, SCHEMA_VERSION);
        }   

    public void onCreate(SQLiteDatabase db) {
         db.execSQL("CREATE TABLE test (username TEXT, password TEXT,)");   }

    public void onUpgrade(SQLiteDatabase db, int arg1, int arg2) {
        String query;
            query = "DROP TABLE IF EXISTS test";
            db.execSQL(query);
            onCreate(db);
        }
    public void InsertNew(String usr, String pswd) {
        SQLiteDatabase db = this.getWritableDatabase();
        db.execSQL("insert into test values('','" + usr + "','" + pswd
                + "')");
       ContentValues cv = new ContentValues();
       cv.put("username", usr);
       cv.put("password", pswd);
       db.insert(testTable, null, cv);
        db.close();
    }

    public Cursor fetchData() {
        SQLiteDatabase db = this.getReadableDatabase();
        Cursor cur = db.rawQuery("select * from test", new String[] {});
        return cur;
    }



}

My app stopped in emulator or bluestack app player...

I can't able to insert and fetch data from database in eclipse android....

Please can anyone help me out to correct or guide me...

Thanks in advance....

Blockquote

Aucun commentaire:

Enregistrer un commentaire