我无法获取数据

时间:2015-10-23 22:04:52

标签: java android

我无法从数据库中获取数据 你能帮帮我吗?

错误日志:

  

1-10091 / com.example.dogruprint.dogruprint2 E / AndroidRuntime:FATAL EXCEPTION:主android.database.sqlite.SQLiteException:没有这样的表:users(代码1):,编译时:SELECT _id,u_name,u_pass从   用户                       在android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native

     
    

方法)                     在android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:882)                     在android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:493)                     在android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)                     在android.database.sqlite.SQLiteProgram。(SQLiteProgram.java:58)                     在android.database.sqlite.SQLiteQuery。(SQLiteQuery.java:37)                     在android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:44)                     在android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1314)                     在android.database.sqlite.SQLiteDatabase.queryWithFactory(SQLiteDatabase.java:1161)                     在android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1032)                     在android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1200)                     在com.example.dogruprint.dogruprint2.Database.getThat(Database.java:123)                     在com.example.dogruprint.dogruprint2.siparisekle $ 2.onClick(siparisekle.java:113)                     在android.view.View.performClick(View.java:4084)                     在android.view.View $ PerformClick.run(View.java:16966)                     在android.os.Handler.handleCallback(Handler.java:615)                     在android.os.Handler.dispatchMessage(Handler.java:92)                     在android.os.Looper.loop(Looper.java:137)                     在android.app.ActivityThread.main(ActivityThread.java:4745)                     at java.lang.reflect.Method.invokeNative(Native Method)                     在java.lang.reflect.Method.invoke(Method.java:511)                     在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:786)                     在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)                     在dalvik.system.NativeStart.main(本地方法)

  

DATABASE.java

package com.example.dogruprint.dogruprint2;

import android.app.Dialog;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.widget.Toast;

import java.sql.SQLException;


public class Database {


    public static final String KEY_ROWID = "_id";
    public static final String KEY_NAME = "u_name";
    public static final String KEY_PASS = "u_pass";

    private static final String DB_NAME = "app";
    private static final String DB_TABLE = "users";
    private static final int version = 1;


    private SQLiteDatabase ourDatabase;
    private DBHelper ourHelper;
    private Context ourContext;
    private SQLiteDatabase writableDatabase;


    public Database(Context context) {

        ourContext = context;

    }

    public SQLiteDatabase getWritableDatabase() {
        return writableDatabase;
    }




    public class DBHelper extends SQLiteOpenHelper {


        public DBHelper(Context context)

        {
            super(context, DB_NAME, null, version);
        }


        @Override
        public void onCreate(SQLiteDatabase db) {


/*

            final String CREATE_TABLE = " CREATE TABLE " + DB_TABLE + " ( " + KEY_ROWID + " INTEGER PRIMARY KEY AUTOINCREMENT , "
                    + KEY_NAME + " VARCHAR(255) , "
                    + KEY_PASS + " VARCHAR(255) ,"
                   ;

*/


            String CREATE_USERS_TABLE = "CREATE TABLE " + DB_TABLE + "(" + KEY_ROWID +
                    " INTEGER PRIMARY KEY," + KEY_NAME + " TEXT," + KEY_PASS + " TEXT"
                    + ");";



        }

        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
            db.execSQL("DROP TABLE IF EXIST " + DB_TABLE);


        }


    }

    public Database open() throws SQLException {


        ourHelper = new DBHelper(ourContext);

        ourDatabase = ourHelper.getWritableDatabase();

        return this;


    }

    public Database close() {

        ourHelper.close();
        return this;


    }

    public void addThat(String name, String pass) {


        ContentValues cv = new ContentValues();

        cv.put(KEY_NAME, name);
        cv.put(KEY_PASS, pass);
        ourDatabase.insert(DB_TABLE, null, cv);


    }

    public String getThat()
    {
        String[] colomns = new String[]{KEY_ROWID,KEY_NAME,KEY_PASS};

        Cursor c = ourDatabase.query(DB_TABLE,colomns,null,null,null,null,null);
        int İROW = c.getColumnIndex(KEY_ROWID);
        int İNAME= c.getColumnIndex(KEY_NAME);
        int İPASS = c.getColumnIndex(KEY_PASS);

        String result = "";

        for(c.moveToFirst();!c.isAfterLast();c.moveToNext())
        {
            result = result + "name" + c.getString(İNAME) + "pass" + c.getString(İPASS)+"\n";


        }



        return result;
    }
}

siparisekle.java

package com.example.dogruprint.dogruprint2;

import android.app.ActionBar;
import android.app.Activity;
import android.app.Dialog;
import android.content.ContentValues;
import android.content.Intent;
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.text.TextUtils;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import java.io.IOException;
import java.sql.SQLException;

import static android.R.*;


public class siparisekle extends Activity {



    private AutoCompleteTextView act1, act2, act3;

    private  EditText edittxt1;

    EditText etName,etPass;
    TextView tvResult;
    Button bSave;
    Button bShow;





    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_siparisekle);

        final Database db = new Database(this);




        install_elements();





        bSave.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {

                boolean ok = true;
                String name = etName.getText().toString();
                String pass = etPass.getText().toString();

                try {
                    db.open();
                    db.addThat(name, pass);
                    tvResult.setText(name + "Eklendi");
                    db.close();

                } catch (Exception e) {
                    ok = false;
                    e.printStackTrace();
                    tvResult.setText("Sorun var");


                } finally {

                    if (ok) {
                        Dialog d = new Dialog(siparisekle.this);
                        TextView tv = new TextView(siparisekle.this);
                        tv.setText("BASARILI");
                        d.setTitle("Sonuç");
                        d.setContentView(tv);
                        d.show();


                    }

                }


            }
        });

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


            {
                try {
                    db.open();
                    String data = db.getThat();
                    tvResult.setText(data);
                    db.close();


                } catch (SQLException e)
                {

                    Toast.makeText(getApplicationContext(),"Çağırmada Bir Sorun Oluştu",Toast.LENGTH_LONG).show();
                    e.printStackTrace();
                }


            }
        });






}
protected void install_elements()
{

    etName = (EditText) findViewById(R.id.etName);
    etPass= (EditText) findViewById(R.id.etPass);
    tvResult= (TextView) findViewById(R.id.tvResult);
    bSave= (Button) findViewById(R.id.bSave);
    bShow= (Button) findViewById(R.id.bShow);

}



}

2 个答案:

答案 0 :(得分:2)

异常消息说明了这一点:

  

没有这样的表:用户(代码1)在编译" SELECT _id,u_name,u_pass FROM users"

它正在寻找一个名为" users"在数据库中,但它找不到一个!

要么您没有正确设置数据库(例如,您还没有创建表),要么您的数据库的架构(例如表名称)与应用程序所需的不匹配。< / p>

事实上,请查看您的onCreate方法。它所做的只是创建并丢弃包含一些SQL的字符串。你需要执行它....

答案 1 :(得分:0)

你真的需要执行你的表创建:

use kartik\file\FileInput;

您还需要在String CREATE_USERS_TABLE = "CREATE TABLE " + DB_TABLE + "(" + KEY_ROWID + " INTEGER PRIMARY KEY," + KEY_NAME + " TEXT," + KEY_PASS + " TEXT" + ")"; db.execSQL(CREATE_USERS_TABLE); 方法

中调用onCreate()方法
onUpgrade()