没有创建SQLite数据库,也没有错误

时间:2015-09-24 13:40:00

标签: java android database eclipse sqlite

没有错误发生时,没有创建SQLite数据库。

我已经检查了android / data文件夹尝试了很多东西。我正在使用eclipse。有人帮助我,并提前感谢。

我的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;
    }

}

2 个答案:

答案 0 :(得分:1)

File file = new File("c:\\tmp\\test.wmv");
java.awt.Desktop.getDesktop().open(file);

像这样创建你的表:

public Database(Context context)
    {
        super(context, DATABASE_NAME , null, 1); // Your Mistake
    }

答案 1 :(得分:0)

试一试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);";