gpt4 book ai didi

java - SQLite 数据库没有被创建并且没有错误出现

转载 作者:行者123 更新时间:2023-12-01 11:07:42 25 4
gpt4 key购买 nike

在没有出现错误的情况下,不会创建 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;
}

}

最佳答案

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);

关于java - SQLite 数据库没有被创建并且没有错误出现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32762913/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com