我也将版本从1更改为2。但仍然没有得到解决。 我的DBAdapter是我创建所有表格的地方
package com.example.mydhara;
import android.content.Context;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
public class DBAdapter {
public static final String DATABASE_NAME = "MyDhara.db";
public static final int DATABASE_VERSION = 2;
//SQL stm to create a Login DB
static String CREATE_TABLE_LOGIN="create table "+"LOGIN"+
"( "+"ID"+" integer primary key autoincrement,"+
"Name text, RegNo text, USERNAME text, PASSWORD text);";
//SQL stm to create a Register DB
static String CREATE_TABLE_REGISTER="create table "+"REGISTER"+
"( "+"ID"+" integer primary key autoincrement,"+
"Name text, RegNo text, EventName text,Branch text, Year integer);";
private final Context context;
private DataBaseHelper dbHelper;
private SQLiteDatabase db;
/**
* Constructor
* @param ctx
*/
public DBAdapter(Context ctx)
{
this.context = ctx;
this.dbHelper = new DataBaseHelper(this.context);
}
private static class DataBaseHelper extends SQLiteOpenHelper
{
DataBaseHelper(Context context)
{
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db)
{
db.execSQL(CREATE_TABLE_LOGIN);
db.execSQL(CREATE_TABLE_REGISTER);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
{
db.execSQL("DROP TABLE IF EXISTS CREATE_TABLE_LOGIN");
db.execSQL("DROP TABLE IF EXISTS CREATE_TABLE_REGISTER");
onCreate(db);
}
}
/**
* open the db
* @return this
* @throws SQLException
* return type: DBAdapter
*/
public DBAdapter open() throws SQLException
{
this.db = this.dbHelper.getWritableDatabase();
return this;
}
/**
* close the db
* return type: void
*/
public void close()
{
this.dbHelper.close();
}
}
我有2个表,这是一个适配器 包com.example.mydhara;
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.widget.Toast;
public class LoginDataBaseAdapter {
private static final String DATABASE_TABLE = "LOGIN";
/*//Column no for each record
public static final int NAME_COLUMN=1;
private static final String DATABASE_NAME = "Mydhara";
private static final int DATABASE_VERSION = 1;*/
//Variable to hold db instance
public SQLiteDatabase db;
//Context of the app using the database
private final Context context;
//DataBase open/upgrade
private DataBaseHelper dbHelper;
private static class DataBaseHelper extends SQLiteOpenHelper {
DataBaseHelper(Context context) {
super(context, DBAdapter.CREATE_TABLE_LOGIN, null, DBAdapter.DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
//db.execSQL(DATABASE_TABLE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// db.execSQL("DROP TABLE IF EXISTS CREATE_TABLE_LOGIN");
}
}
public LoginDataBaseAdapter(Context _context)
{
this.context=_context;
//dbHelper = new DataBaseHelper(context, DATABASE_NAME, null, DATABASE_VERSION);
}
//Opening database
public LoginDataBaseAdapter open() throws SQLException
{
this.dbHelper = new DataBaseHelper(this.context);
this.db=this.dbHelper.getWritableDatabase();
return this;
}
//closing the database
public void close()
{
db.close();
}
//getting an instance of db
public SQLiteDatabase getDatabaseInstance()
{
return db;
}
//Inserting a value
public void insertEntry(String name,String regno, String userName,String password)
{
ContentValues newvalues=new ContentValues();
//Assign values for each row
newvalues.put("Name", name);
newvalues.put("RegNo", regno);
newvalues.put("USERNAME", userName);
newvalues.put("PASSWORD", password);
//inserting
db.insert(DATABASE_TABLE, null, newvalues);
Toast.makeText(context, "Successfully Inserted", 7000).show();
}
//Deleting a value
public int deleteEntry(String UserName)
{
int no_of_records=db.delete("LOGIN", "USERNAME=?", new String[]{UserName});
Toast.makeText(context, "No of Enteries Deleted are "+no_of_records, 7000).show();
return no_of_records;
}
//Getting single entry for a query
public String getSingleEntry(String UserName)
{
Cursor cursor=db.query(DATABASE_TABLE,null," USERNAME=?",new String[]{UserName},null,null,null);
if(cursor.getCount()<1)
{
cursor.close();
return "Not EXIST";
}
cursor.moveToFirst();
String password=cursor.getString(cursor.getColumnIndex("PASSWORD"));
cursor.close();
return password;
}
//getting single username
public String getSingleUs(String UserName)
{
Cursor cursor =db.query(DATABASE_TABLE,null,"USERNAME=?",new String[]{UserName},null,null,null);
if(cursor.getCount()<1)
{
cursor.close();
return "Not EXIST";
}
cursor.moveToFirst();
String userName=cursor.getString(cursor.getColumnIndex("USERNAME"));
cursor.close();
return UserName;
}
//Update a value
public void updateEntry(String userName,String password)
{
ContentValues updateValues=new ContentValues();
updateValues.put("PASSWORD", password);
db.update(DATABASE_TABLE, updateValues, "USERNAME=?", new String[]{userName});
}
}
登录(Main.java)
package com.example.mydhara;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class Main extends Activity implements OnClickListener {
Button blogin,bsignup;
TextView tvUser,tvPass;
EditText etUser,etPass;
LoginDataBaseAdapter loginDataBaseAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//get instance of db adapter
loginDataBaseAdapter = new LoginDataBaseAdapter(this);
loginDataBaseAdapter = loginDataBaseAdapter.open();
//initialising all the buttons,textview and EditText
blogin= (Button) findViewById(R.id.blogin);
bsignup= (Button) findViewById(R.id.bsignup);
etUser= (EditText) findViewById(R.id.etUser);
etPass= (EditText) findViewById(R.id.etPass);
blogin.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
String us=etUser.getText().toString();
String ps=etPass.getText().toString();
if(us.equals("")||ps.equals(""))
{
Toast.makeText(getApplicationContext(), "The field are empty", 7000).show();
etUser.setText("");
etPass.setText("");
return ;
}
String storedUs = loginDataBaseAdapter.getSingleUs(us);
if(us.equals(storedUs))
{
Toast.makeText(getApplicationContext(), "the username already exists", 7000).show();
etUser.setText("");
etPass.setText("");
}
String storedpassword = loginDataBaseAdapter.getSingleEntry(us);
if(ps.equals(storedpassword))
{
Toast.makeText(getApplicationContext(), "Successfully Logged In", 7000).show();
Intent i = new Intent(getApplicationContext(),SelectForum.class);
startActivity(i);
return ;
}
else
{
Toast.makeText(getApplicationContext(), "The username or password entered is invalid", 7000).show();
etUser.setText("");
etPass.setText("");
return ;
}
}
});
bsignup.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
//Toast.makeText(getApplicationContext(), "Signup", Toast.LENGTH_LONG).show();
Intent i = new Intent(getApplicationContext(),Signup.class);
startActivity(i);
}
});
}
@Override
protected void onDestroy() {
super.onDestroy();
loginDataBaseAdapter.close();
}
@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;
}
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
}}
logcat的。
04-13 09:54:42.560: I/SqliteDatabaseCpp(2075): sqlite returned: error code = 1, msg = no such table: LOGIN, db=/data/data/com.example.mydhara/databases/create table LOGIN( ID integer primary key autoincrement,Name text, RegNo text, USERNAME text, PASSWORD text);
04-13 09:54:42.630: E/SQLiteDatabase(2075): Error inserting Name=Admin USERNAME=admin PASSWORD=admin RegNo=cse11007
04-13 09:54:42.630: E/SQLiteDatabase(2075): android.database.sqlite.SQLiteException: no such table: LOGIN: , while compiling: INSERT INTO LOGIN(Name,USERNAME,PASSWORD,RegNo) VALUES (?,?,?,?)
04-13 09:54:42.630: E/SQLiteDatabase(2075): at android.database.sqlite.SQLiteCompiledSql.native_compile(Native Method)
04-13 09:54:42.630: E/SQLiteDatabase(2075): at android.database.sqlite.SQLiteCompiledSql.<init>(SQLiteCompiledSql.java:68)
04-13 09:54:42.630: E/SQLiteDatabase(2075): at android.database.sqlite.SQLiteProgram.compileSql(SQLiteProgram.java:143)
04-13 09:54:42.630: E/SQLiteDatabase(2075): at android.database.sqlite.SQLiteProgram.compileAndbindAllArgs(SQLiteProgram.java:361)
04-13 09:54:42.630: E/SQLiteDatabase(2075): at android.database.sqlite.SQLiteStatement.acquireAndLock(SQLiteStatement.java:260)
04-13 09:54:42.630: E/SQLiteDatabase(2075): at android.database.sqlite.SQLiteStatement.executeInsert(SQLiteStatement.java:112)
04-13 09:54:42.630: E/SQLiteDatabase(2075): at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1718)
04-13 09:54:42.630: E/SQLiteDatabase(2075): at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1591)
04-13 09:54:42.630: E/SQLiteDatabase(2075): at com.example.mydhara.LoginDataBaseAdapter.insertEntry(LoginDataBaseAdapter.java:92)
04-13 09:54:42.630: E/SQLiteDatabase(2075): at com.example.mydhara.Signup$1.onClick(Signup.java:75)
04-13 09:54:42.630: E/SQLiteDatabase(2075): at android.view.View.performClick(View.java:3511)
04-13 09:54:42.630: E/SQLiteDatabase(2075): at android.view.View$PerformClick.run(View.java:14105)
04-13 09:54:42.630: E/SQLiteDatabase(2075): at android.os.Handler.handleCallback(Handler.java:605)
04-13 09:54:42.630: E/SQLiteDatabase(2075): at android.os.Handler.dispatchMessage(Handler.java:92)
04-13 09:54:42.630: E/SQLiteDatabase(2075): at android.os.Looper.loop(Looper.java:137)
04-13 09:54:42.630: E/SQLiteDatabase(2075): at android.app.ActivityThread.main(ActivityThread.java:4424)
04-13 09:54:42.630: E/SQLiteDatabase(2075): at java.lang.reflect.Method.invokeNative(Native Method)
04-13 09:54:42.630: E/SQLiteDatabase(2075): at java.lang.reflect.Method.invoke(Method.java:511)
04-13 09:54:42.630: E/SQLiteDatabase(2075): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:825)
04-13 09:54:42.630: E/SQLiteDatabase(2075): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:592)
04-13 09:54:42.630: E/SQLiteDatabase(2075): at dalvik.system.NativeStart.main(Native Method)
04-13 09:54:50.920: I/SqliteDatabaseCpp(2075): sqlite returned: error code = 1, msg = no such table: LOGIN, db=/data/data/com.example.mydhara/databases/create table LOGIN( ID integer primary key autoincrement,Name text, RegNo text, USERNAME text, PASSWORD text);
04-13 09:54:50.930: D/AndroidRuntime(2075): Shutting down VM
04-13 09:54:50.930: W/dalvikvm(2075): threadid=1: thread exiting with uncaught exception (group=0xb2ce6180)
04-13 09:54:50.930: D/AndroidRuntime(2075): procName from cmdline: com.example.mydhara
04-13 09:54:50.930: E/AndroidRuntime(2075): in writeCrashedAppName, pkgName :com.example.mydhara
04-13 09:54:50.930: D/AndroidRuntime(2075): file written successfully with content: com.example.mydhara StringBuffer : ;com.example.mydhara
04-13 09:54:50.940: I/Process(2075): Sending signal. PID: 2075 SIG: 9
04-13 09:54:50.940: E/AndroidRuntime(2075): FATAL EXCEPTION: main
04-13 09:54:50.940: E/AndroidRuntime(2075): android.database.sqlite.SQLiteException: no such table: LOGIN: , while compiling: SELECT * FROM LOGIN WHERE USERNAME=?
04-13 09:54:50.940: E/AndroidRuntime(2075): at android.database.sqlite.SQLiteCompiledSql.native_compile(Native Method)
04-13 09:54:50.940: E/AndroidRuntime(2075): at android.database.sqlite.SQLiteCompiledSql.<init>(SQLiteCompiledSql.java:68)
04-13 09:54:50.940: E/AndroidRuntime(2075): at android.database.sqlite.SQLiteProgram.compileSql(SQLiteProgram.java:143)
04-13 09:54:50.940: E/AndroidRuntime(2075): at android.database.sqlite.SQLiteProgram.compileAndbindAllArgs(SQLiteProgram.java:361)
04-13 09:54:50.940: E/AndroidRuntime(2075): at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:127)
04-13 09:54:50.940: E/AndroidRuntime(2075): at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:94)
04-13 09:54:50.940: E/AndroidRuntime(2075): at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:53)
04-13 09:54:50.940: E/AndroidRuntime(2075): at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:47)
04-13 09:54:50.940: E/AndroidRuntime(2075): at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1564)
04-13 09:54:50.940: E/AndroidRuntime(2075): at android.database.sqlite.SQLiteDatabase.queryWithFactory(SQLiteDatabase.java:1449)
04-13 09:54:50.940: E/AndroidRuntime(2075): at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1405)
04-13 09:54:50.940: E/AndroidRuntime(2075): at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1485)
04-13 09:54:50.940: E/AndroidRuntime(2075): at com.example.mydhara.LoginDataBaseAdapter.getSingleUs(LoginDataBaseAdapter.java:123)
04-13 09:54:50.940: E/AndroidRuntime(2075): at com.example.mydhara.Main$1.onClick(Main.java:57)
04-13 09:54:50.940: E/AndroidRuntime(2075): at android.view.View.performClick(View.java:3511)
04-13 09:54:50.940: E/AndroidRuntime(2075): at android.view.View$PerformClick.run(View.java:14105)
04-13 09:54:50.940: E/AndroidRuntime(2075): at android.os.Handler.handleCallback(Handler.java:605)
04-13 09:54:50.940: E/AndroidRuntime(2075): at android.os.Handler.dispatchMessage(Handler.java:92)
04-13 09:54:50.940: E/AndroidRuntime(2075): at android.os.Looper.loop(Looper.java:137)
04-13 09:54:50.940: E/AndroidRuntime(2075): at android.app.ActivityThread.main(ActivityThread.java:4424)
04-13 09:54:50.940: E/AndroidRuntime(2075): at java.lang.reflect.Method.invokeNative(Native Method)
04-13 09:54:50.940: E/AndroidRuntime(2075): at java.lang.reflect.Method.invoke(Method.java:511)
04-13 09:54:50.940: E/AndroidRuntime(2075): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:825)
04-13 09:54:50.940: E/AndroidRuntime(2075): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:592)
04-13 09:54:50.940: E/AndroidRuntime(2075): at dalvik.system.NativeStart.main(Native Method)