无法在SQLite中看到创建的表

时间:2017-09-02 21:24:53

标签: android sqlite android-sqlite

我正在尝试在android studio中创建一个表,当我转到ADB工具并转到SQLITE3时...我可以进入我的数据库但是当我从命令行写.tables时没有显示出来。这是我创建数据库及其表的代码。

public class DatabaseOperations extends SQLiteOpenHelper
{
    public static final String NAME = "event_name";
    public static final String LOCATION = "event_location";
    public static final String DATE = "event_date";
    public static final String TIME = "event_time";

    public static final String DATABASE_NAME = "events_stuff.db";
    public static final String TABLE_NAME = "events";
    public static final int database_version = 7;
    public String CREATE_QUERY = "CREATE TABLE " + TABLE_NAME+"( ID INTEGER PRIMARY KEY, "+ NAME + " TEXT,"
            + LOCATION + " TEXT,"+DATE+" TEXT,"+TIME+" TEXT);";

    public DatabaseOperations(Context context)
    {
        super(context, DATABASE_NAME, null, database_version);
        Log.d("Database operations", "Database created");

    }


    @Override
    public void onCreate(SQLiteDatabase sdb)
    {
        sdb.execSQL(CREATE_QUERY);
        Log.d("Database operations", "Table Created");
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("DROP TABLE IF EXISTS " + DATABASE_NAME);
        onCreate(db);
    }
    public void addProduct(Events e) {

        ContentValues values = new ContentValues();
        values.put(NAME, e.get_eventname());
        values.put(LOCATION, e.get_eventloc());
        values.put(DATE, e.get_eventdate());
        values.put(TIME, e.get_eventdate());
        SQLiteDatabase db = this.getWritableDatabase();

        db.insert(TABLE_NAME, null, values);
        db.close();
    }
}

然后在我的片段中...我尝试将信息插入表中,如下所示:

public static EditText eventName;
public static EditText eventLoc;
public static EditText eventDate;
public static EditText eventTime;
public static DatabaseOperations mDBHelper;
@Override
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setHasOptionsMenu(true);
    mDBHelper = new DatabaseOperations(getActivity().getApplicationContext());
    mDBHelper.getWritableDatabase();
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch(item.getItemId())
    {
        case R.id.save:
            newProduct();
            Toast.makeText(getActivity().getApplicationContext(), "YAY YOUR EVENT HAS INSERTED", Toast.LENGTH_LONG).show();
            return true;
    }
    return false;
}
public void newProduct ()
{
    DatabaseOperations dbHandler = new DatabaseOperations(getActivity().getApplicationContext());
    Events events =
            new Events(eventName.getText().toString(), eventLoc.getText().toString(), eventDate.getText().toString(), eventTime.getText().toString());
    dbHandler.addProduct(events);

}

我的事件类定义如下:

public class Events
{

    private int _id;
    private String _eventname;
    private String _eventloc;
    private String _eventdate;
    private String _eventtime;

    public Events(){}

    public Events(String _eventname, String _eventloc, String _eventdate, String _eventtime)
    {
        this._eventname = _eventname;
        this._eventloc = _eventloc;
        this._eventdate = _eventdate;
        this._eventtime = _eventtime;
    }

    public void set_id(int _id)
    {
        this._id = _id;
    }

    public void set_eventname(String _eventname)
    {
        this._eventname = _eventname;
    }

    public void set_eventloc(String _eventloc)
    {
        this._eventloc = _eventloc;
    }

    public void set_eventdate(String _eventdate)
    {
        this._eventdate = _eventdate;
    }

    public void set_eventtime(String _eventtime)
    {
        this._eventtime = _eventtime;
    }

    public int get_id()
    {
        return _id;
    }

    public String get_eventname()
    {
        return _eventname;
    }

    public String get_eventloc()
    {
        return _eventloc;
    }

    public String get_eventdate()
    {
        return _eventdate;
    }

    public String get_eventtime()
    {
        return _eventtime;
    }


}

当我尝试插入我的数据时,它说它正在工作但是当我进入命令行时甚至没有表格出现。我如何创建一个表,如果我已经创建了一个表,我在哪里可以看到该信息?我已经梳理了很多youtube视频和stackoverflow帖子,但没有一个有效。

谢谢!

1 个答案:

答案 0 :(得分:0)

1)在DatabaseOperations中添加方法: -

public Cursor getAllRows() {
    SQLiteDatabase db = getWritableDatabase();
    return db.query(TABLE_NAME,null,null,null,null,null,null);
}

2)在 newProduct() ;

之后的片段中
   Cursor csr = mDBHelper.getAllRows();
   Log.d("PRODUCTS","# of Rows=" + Integer.toString(csr.getCount());
   while (csr.moveToNext) {
       Log.d("PRODUCTS",
           "Row=" + csr.getString(1)
       );
   }
   csr.close();

这应该列出行数,以及每行的NAME(event_name列),如果有的话。

根据所使用的设备,可能会限制访问以查看数据库。虽然您可以复制数据库。我使用的是SQliteManager,它是一个浏览器扩展(可能还有其他类似的产品)。

注意我没有对上面进行测试,所以可能会出现奇怪的错字。为简洁起见,我还使用了NAME / event_name列的索引/偏移量。理想情况下,您可以使用列名来获取索引,例如代替"Row=" + csr.getString(1)而不是代码

"Row=" + csr.getString(csr.getColumnIndex(DatabaseOperations.NAME))