"table doesn't exist" error with sqlite
I am having problem with the database access in android. First, I created an activity class for creating a table in the database. Then I called that activity from another. Now on compiling I am stuck with an error that the table doesn't exists. Here is the code.
the first class is:-
package bivin.hello;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.widget.EditText;
import android.widget.Toast;
public class main1 extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Intent i=new Intent();
startActivity(i);
SQLiteDatabase db;
db=(SQLiteDatabase)openOrCreateDatabase("bivin.db", SQLiteDatabase.OPEN_READONLY,null);
Cursor cur=db.query("emp", null, null, null, null,null,null);
db.beginTransaction();
cur.moveToPosition(1);
db.close();
and the calling activity is
package com.example.android.apis;
import android.app.Activity;
import android.content.ContentValues;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
public class createtable extends Activity
{
public void onCreate(Bundle b)
{
super.onCreate(b);
开发者_Python百科 SQLiteDatabase db;
db=openOrCreateDatabase("bivin.db", SQLiteDatabase.CREATE_IF_NECESSARY, null);
db.execSQL("create table emp(name text,password text;");
ContentValues c=new ContentValues();
c.put("name", "Bivin");
c.put("password", "Ravindran");
db.insert("emp",null,c);
db.close();
}
}
}
}
Its obvious, createtable onCreate should be executed before main1's onCreate. The table is being created later, hence the error
your datatype is wrong use varchar instread of text
SQLiteDatabase sampleDB = null;
String PhoneNumber;
String UserId;
Cursor c;
ArrayList ar=new ArrayList();
public void createDatabse(String DbName,Context context)
{
sampleDB = context.openOrCreateDatabase(DbName, context.MODE_WORLD_READABLE,null);
}
public void createUserIdTable(String Tablename,Context context)
{
sampleDB.execSQL("CREATE TABLE IF NOT EXISTS " +
Tablename +
" (User_Id VARCHAR);");
}
public void insertDataUser(String tableName,String User_Id)
{
sampleDB.execSQL("INSERT INTO " +
tableName +
" Values ('"+User_Id+"');");
}
public String GetUserData(Context context,String tablename)
{
c = sampleDB.rawQuery("SELECT User_Id FROM " +
tablename +
"", null);
if(c!=null)
{
if(c.moveToFirst())
{
do
{
UserId=c.getString(c.getColumnIndex("User_Id"));
}while(c.moveToNext());
}
}
return UserId;
}
public void close()
{
sampleDB.close();
}
精彩评论