我在android中遇到数据库访问问题。 首先,我创建了一个用于在数据库中创建表的活动类。 然后我从另一个人那里调用了那个活现在在编译时我遇到了表不存在的错误。这是代码。
第一堂课是: -
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();
,调用活动是
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);
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();
}
}
}
}
答案 0 :(得分:0)
显而易见的,可创建的onCreate应该在main1的onCreate之前执行。该表稍后创建,因此错误
答案 1 :(得分:0)
您的数据类型错误使用varchar而不是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();
}