无法在Eclipse for Android中查找数据库文件

时间:2014-07-14 07:09:22

标签: java android mysql database eclipse

我最近一直在做一个android数据库应用程序项目。当我试图执行时。我无法在DDMS的文件浏览器中找到文件夹'databases'。我检查了以下路径/数据/数据/包名/数据库,但在那里我找不到数据库文件夹和我的数据库文件。

我附加了数据库助手类的代码 如果有任何错误阻止我创建数据库或者我的日食错误,请帮助我。因为即使我现有的一些项目也没有出现在其中的数据库文件夹中。

DBclass.java:

package pack.andyxdb;

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

public class DBclass {

    public static final String KEY_TIMESTAMP = "Timestamp";
    public static final String KEY_UNIQUEID = "UniqueID";
    public static final String KEY_DEVICETYPE = "Devicetype";
    public static final String KEY_RSSI = "RSSI";

    private static final String DATABASE_NAME = "DannyZ.db";
    private static final String DATABASE_TABLE = "Data";
    private static final int DATABASE_VERSION = 1;

    private final Context ourContext;
    private DbHelper dbh;
    private SQLiteDatabase odb;

    private static final String USER_MASTER_CREATE =
            "CREATE TABLE IF NOT EXISTS " + DATABASE_TABLE+ "("
                    + KEY_TIMESTAMP + " INTEGER ,"
                    + KEY_UNIQUEID  + " INTEGER, " + KEY_DEVICETYPE + " TEXT, " + KEY_RSSI + " INTEGER PRIMARY KEY )";
    //CREATE TABLE `DannyZ` (

    private static class DbHelper extends SQLiteOpenHelper {

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

        @Override
        public void onCreate(SQLiteDatabase db) {
            db.execSQL(USER_MASTER_CREATE);
        }

        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
            // if DATABASE VERSION changes
            // Drop old tables and call super.onCreate()
        }
    }

    public DBclass(Context c) {
        ourContext = c;
        dbh = new DbHelper(ourContext);
    }

    public DBclass open() throws SQLException {
        odb = dbh.getWritableDatabase();
        return this;
    }

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

    public long insertrecord(int timestamp, int uniqueID,String Device, int RSSI) throws SQLException{
        // Log.d("", col1);
        // Log.d("", col2);

        ContentValues IV = new ContentValues();

        IV.put(KEY_TIMESTAMP, timestamp);
        IV.put(KEY_UNIQUEID, uniqueID );
        IV.put(KEY_DEVICETYPE, Device != "");
        IV.put(KEY_RSSI, RSSI );

        return odb.insert(DATABASE_TABLE, null, IV);
        // returns a number >0 if inserting data is successful
    }

    public void updateRow(long rowID, String col1, String col2,String col3,String col4) {
        ContentValues values = new ContentValues();
        values.put(KEY_TIMESTAMP, col1);
        values.put(KEY_UNIQUEID, col2);
        values.put(KEY_DEVICETYPE, col3);
        values.put(KEY_RSSI, col4);

        try {
            odb.update(DATABASE_TABLE, values, KEY_RSSI + "=" + rowID, null);
        } catch (Exception e) {
        }
    }

    public boolean delete() {
        return odb.delete(DATABASE_TABLE, null, null) > 0;
    }

    public Cursor getAllTitles() {
        // using simple SQL query
        return odb.rawQuery("select * from " + DATABASE_TABLE + "ORDER BY "+KEY_RSSI, null);
    }

    public Cursor getallCols(String id) throws SQLException {
        Cursor mCursor = odb.query(DATABASE_TABLE, new String[] { KEY_TIMESTAMP,
                KEY_UNIQUEID, KEY_DEVICETYPE, KEY_RSSI }, null, null, null, null, null);
        Log.e("getallcols zmv", "opening successfull");
        return mCursor;
    }

    public Cursor getColsById(String id) throws SQLException {
        Cursor mCursor = odb.query(DATABASE_TABLE, new String[] { KEY_TIMESTAMP,
                KEY_UNIQUEID,KEY_DEVICETYPE }, KEY_RSSI + " = " + id, null, null, null, null);
        Log.e("getallcols zmv", "opening successfull");
        return mCursor;
    }
}

我的MainActivity.java代码也在这里。在这里,我尝试使用提交按钮将数据插入数据库。当我按下提交按钮时,应用程序不会停留很长时间并且说不幸的是myapp已经停止。我很关心我的数据库是否被创建?

请帮助我谢谢

MainActivity:

public class MainActivity extends Activity {

    private ListView list_lv;
    private EditText txt1;
    private EditText txt2;
    private EditText txt3;
    private EditText txt4;
    private Button btn1;
    private Button btn2;
    private DBclass db;

    private ArrayList<String> collist_1;
    private ArrayList<String> collist_2;
    private ArrayList<String> collist_3;
    private ArrayList<String> collist_4;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        collist_1 = new ArrayList<String>();
        collist_2 = new ArrayList<String>();
        collist_3 = new ArrayList<String>();
        collist_4 = new ArrayList<String>();

        items();
        // getData();
    }

    private void items() {
        btn1 = (Button) findViewById(R.id.button1);
        btn2 = (Button) findViewById(R.id.button2);
        txt1 = (EditText) findViewById(R.id.editText1);
        txt2 = (EditText) findViewById(R.id.editText2);
        txt3 = (EditText) findViewById(R.id.editText3);
        txt4 = (EditText) findViewById(R.id.editText4);
        // list_lv = (ListView) findViewById(R.id.dblist);

        btn2.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                //getData();
                viewData();
            }

            private void viewData() {
                Intent i = new Intent(MainActivity.this, viewActivity.class);
                startActivity(i);


            }
        });

        btn1.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                submitData();
            }
        });
    }

    protected void submitData() {
        int a = Integer.parseInt( txt1.getText().toString());
        int b = Integer.parseInt( txt2.getText().toString());
        String c = txt3.getText().toString();
        int d = Integer.parseInt( txt4.getText().toString());

        db = new DBclass(this);

        long num = 0;
        try {
            db.open();
            num = db.insertrecord(a, b,c,d);
            db.close();
        } catch (SQLException e) {
            Toast.makeText(this, "Error Duplicate value"+e,2000).show();

        } finally {
            //getData();
        }
        if (num > 0)
            Toast.makeText(this, "Row number: " + num, 2000).show();
        else if (num == -1)
            Toast.makeText(this, "Error Duplicate value", 4000).show();
        else
            Toast.makeText(this, "Error while inserting", 2000).show();
    }
}

1 个答案:

答案 0 :(得分:-2)

在您的activity onCreate方法中创建辅助类的对象,以便在Android应用程序中创建数据库文件

DBclass db=new DBclass(context);

希望这会有所帮助。