【问题标题】:SQLite database is not being created and no error coming没有创建 SQLite 数据库,也没有错误出现
【发布时间】:2015-09-24 13:39:59
【问题描述】:

SQLite 数据库没有被创建,但没有出现错误。

我已经检查了 android/data 文件夹并尝试了很多方法。我正在使用日食。有人请帮助我,并在此先感谢。

我的Database.java 文件是这样的:

public class Database extends SQLiteOpenHelper {

    public static final String DATABASE_NAME="Table.db";
    public static final String TABLE_NAME="table_one";
    public static final String COL_1="ID";
    public static final String COL_2="NAME";
    public static final String COL_3="SURNAME";
    public static final String COL_4="MARKS";

    public Database(Context context) {
        super(context, TABLE_NAME, null, 1);
        // TODO Auto-generated constructor stub
    }

    @Override
    public void onCreate(SQLiteDatabase arg) {
        // TODO Auto-generated method stub
        arg.execSQL("create table "+TABLE_NAME+"(ID INTEGER PRIMARY KEY AUTOINCREMENT, NAME TEXT, SURNAME TEXT, MARKS INTEGER)");
    }

    @Override
    public void onUpgrade(SQLiteDatabase arg, int arg1, int arg2) {
        // TODO Auto-generated method stub
        arg.execSQL("DROP TABLE IF EXISTS "+TABLE_NAME);
        onCreate(arg);
    }

    public boolean insert(String name, String sname, String marks)
    {
        SQLiteDatabase arg=this.getWritableDatabase();
        ContentValues content=new ContentValues();
        content.put(COL_2, name);
        content.put(COL_3, sname);
        content.put(COL_4, marks);
        long result=arg.insert(TABLE_NAME, null, content);
        if(result==-1)
            return false;
        else
        return true;

    }
}

MainActivity.java 是:

public class MainActivity extends Activity {

    Database myDb;
    EditText name, sname, marks;
    Button add;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        myDb = new Database(this);
        name = (EditText) findViewById(R.id.ename);
        sname = (EditText) findViewById(R.id.esname);
        marks = (EditText) findViewById(R.id.emarks);
        add = (Button) findViewById(R.id.button1);
        addData();
    }

    public void addData() {
        add.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                boolean isInserted = myDb.insert(name.getText().toString(), sname.getText().toString(), marks.getText().toString());
                if (isInserted == true)
                    Toast.makeText(MainActivity.this, "Data is inserted", Toast.LENGTH_LONG).show();
                else
                    Toast.makeText(MainActivity.this, "Data is NOT inserted", Toast.LENGTH_LONG).show();
            }
        });
    }

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

}

【问题讨论】:

  • 在左括号前留一个空格,如TABLE_NAME+" (,它应该是_id 而不是id
  • 将表名更改为数据库名 super(context, DATABASE_NAME, null, 1) 并引用 stackoverflow.com/questions/21881992/…
  • 我将它从 TABLE_NAME 更改为 DATABASE_NAME 仍然没有效果,Blackbelt 可以指定 _id 而不是 id- 吗?

标签: java android database eclipse sqlite


【解决方案1】:
public Database(Context context)
    {
        super(context, DATABASE_NAME , null, 1); // Your Mistake
    }

像这样创建你的表:

String CREATE_TABLE = "CREATE TABLE " + GROUP_CHAT_MESSAGE_TABLE_NAME + "("
                + ID + " INTEGER PRIMARY KEY," 
                + NAME + " TEXT,"
                + SURNAME + " TEXT ,"                                   
                + MARKS + " INTEGER" + ")";

db.execSQL(CREATE_TABLE);

【讨论】:

  • 仍然无法正常工作。主要的是,当我单击添加按钮时,我收到消息“数据已插入”,但是当我转到 Android->Data 时,没有看到我的包的文件夹。
  • 只能在模拟器中查看数据
  • 为了节省时间,我使用智能手机而不是模拟器来启动我的应用程序。
  • 您无法在移动设备中检查您的 sqlite 数据库,您可以使用比模拟器更快的 GenyMotion 代替模拟器。
【解决方案2】:

试试看here。但是,您应该使用 DAO 类;这样您就可以通过一个中间对象来管理数据库中的信息以及发送给它的信息。

尝试像这样创建表:

private static final String DATABASE_CREATE = "create table "
        + DATABASE_NAME + "(" + COL_1 +
        " integer primary key autoincrement, "  +
        COL_2 + " text not null, "  + COL_3 + " text not null, " + COL_4 + " integer);";

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-04-01
    • 1970-01-01
    • 2018-08-20
    • 1970-01-01
    • 2017-03-08
    • 1970-01-01
    • 2014-02-15
    相关资源
    最近更新 更多