I write SQL code at small android application, always database create successfully, but that is not correct for the table.
public dbSqlHelper dbHelper;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
dbHelper = new dbSqlHelper(this);
SQLiteDatabase dataSql = dbHelper.getWritableDatabase();
}
that is the main, and the following code for dbhelper
private static final String DATABASE_NAME = "malekData.db";
private static final int DATABASE_VERSION = 1;
private Context context;
public dbSqlHelper(#Nullable Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
#Override
public void onCreate( SQLiteDatabase db) {
try {
db.execSQL("CREATE TABLE malekDB4 ( id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT);");
} catch (SQLException e) {
Log.e("Malek", "Error when creating table: " + e.getMessage());
}
}
#Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
when I go to terminal and check table (.tables), no thing return
Related
I am trying to make loging page but this code not run properly
application afer run tym says unfortunately has stopped i don't no why .i am new in android any one plzz help .i am so frustrate
database file!!
public class RegistrationDetailsDataBaseFile extends SQLiteOpenHelper {
public static final String DATABASE_NAME="registrationDataBase.db";
public static final String TABLE_NAME="registrationTable";
public static final String NAME="name";
public static final String PASSWORD="password";
public static final String PHONE="phone";
public static final String ADDRESS="address";
private static final String QUERY="CREATE TABLE "+TABLE_NAME+"(id integer primary key autoincrement"+NAME+"not null"+PASSWORD+"not null"+PHONE+"not null"+ADDRESS+"not null)";
RegistrationDetailsDataBaseFile(Context context){
super(context,DATABASE_NAME,null,1);
}
public void onCreate(SQLiteDatabase db){
db.execSQL(QUERY);
}
public void onUpgrade(SQLiteDatabase db,int oldVersion,int newVersion)
{
db.execSQL("drop table if exists"+TABLE_NAME);
onCreate(db);
}
public void setName(String nam){
SQLiteDatabase db=this.getWritableDatabase();
ContentValues contentValues=new ContentValues();
contentValues.put(NAME,nam);
db.insert(TABLE_NAME,null,contentValues);
db.close();
}
}
send data from this file file
public class MainActivity extends AppCompatActivity {
EditText mEditTextName,mEditTextPassword,mEditTextPhone,mEditTextAddress;
Button mButtonRegistration;
String getName,getPassword,getPhone,getAddress;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mEditTextName=(EditText)findViewById(R.id.name);
mEditTextPassword=(EditText)findViewById(R.id.password);
mEditTextPhone=(EditText)findViewById(R.id.phone);
mEditTextAddress=(EditText)findViewById(R.id.address);
mButtonRegistration=(Button)findViewById(R.id.register);
mButtonRegistration.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
getName=mEditTextName.getText().toString();
getPassword=mEditTextPassword.getText().toString();
getPhone=mEditTextPhone.getText().toString();
getAddress=mEditTextAddress.getText().toString();
RegistrationDetailsDataBaseFile objOfRegistrationDetailsDataBaseFile=new RegistrationDetailsDataBaseFile(getBaseContext());
objOfRegistrationDetailsDataBaseFile.setName(getName);
Toast.makeText(getBaseContext(), "name", Toast.LENGTH_LONG).show();
}
});
}
}
You didn't define any type of the columns. There is also spacing problems. To create table modify your query statement as following:
private static final String QUERY="CREATE TABLE "+TABLE_NAME+" (id integer primary key autoincrement, "+NAME+" TEXT NOT NULL, "+PASSWORD+" TEXT NOT NULL, "+PHONE+" TEXT NOT NULL, "+ADDRESS+"TEXT NOT NULL);";
Also modify the onUpgrade() method as following:
public void onUpgrade(SQLiteDatabase db,int oldVersion,int newVersion)
{
db.execSQL("drop table if exists "+TABLE_NAME);
onCreate(db);
}
im trying to do a system.out.println("something") inside onCreate() for SQLiteOpenHelper. but i didn't get anything. i even made sure to call getReadableDatabase();
anyone knows why?
this is my sqlite class
public class ChatOpenHelper extends SQLiteOpenHelper {
private static final int DATABASE_VERSION = 2;
public static final String DATABASE_NAME = "FeedReader.db";
private static final String CHAT_TABLE_NAME = "Bolster_ME_YOU";
private static final String CHAT_TABLE_CREATE =
"CREATE TABLE IF NOT EXISTS " + CHAT_TABLE_NAME + " (" +
" test VARCHAR(255) );";
private static final String CHAT_TABLE_DELETE =
"DROP TABLE IF NOT EXISTS " + CHAT_TABLE_NAME + ");";
public ChatOpenHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
#Override
public void onCreate(SQLiteDatabase db) {
System.out.println("hello from sqlite");
System.out.println(this.getDatabaseName());
db.execSQL(CHAT_TABLE_DELETE);
db.execSQL(CHAT_TABLE_CREATE);
Log.v("smartdbhelper", "after creation");
}
#Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
this.onCreate(db);
}
}
//this is the activity class
public class LoginActivity extends ActionBarActivity {
private Button signin;
private Button signup;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
signin = (Button)findViewById(R.id.login_signin);
signup = (Button)findViewById(R.id.login_signup);
ChatOpenHelper open = new ChatOpenHelper(this);
SQLiteDatabase sql = open.getReadableDatabase();
sql.close();
open.close();
}
}
SQLiteOpenHelper.onCreate() is only called when the database is created for the first time.
You'd better use Log.d("label", "value") rather than System.out.print().
I am trying to insert data in the table MyProduct but every time i run the program it shows "null pointer exception" in db.insertData() in MainActivity and method insertData() in MyHelper class. Can anyone please tell me whats going wrong?? Thank You
MainActivity
public class MainActivity extends Activity {
MyHelper helper;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
helper=new MyHelper(this);
SQLiteDatabase sqLiteDatabase=helper.getWritableDatabase();
// helper.onCreate(sqLiteDatabase);
helper.insertData();
}
}
MyHelper.class
public class MyHelper extends SQLiteOpenHelper {
static final String DATABASE_NAME="MyDatabase";
static final int DATABASE_VERSION=1;
static final String TABLE_NAME="MyCatalog";
static final String UID="ID";
static final String UTITLE="TITLE";
static final String UPRICE="PRICE";
static final String UDESCP="DESCP";
static final String CREATE_TABLE="CREATE TABLE"+TABLE_NAME+"("+UID+"VARCHAR(25) PRIMARY KEY,"+UTITLE+ " VARCHAR(255),"+UPRICE+" VARCHAR(255),"+UDESCP+" VARCHAR(255));";
private static final String DROP_TABLE="DROP TABLE" +TABLE_NAME+"IF EXISTS";
private Context context;
public MyHelper(Context context)
{
super(context,DATABASE_NAME,null,DATABASE_VERSION);
this.context=context;
Message.message(context,"Constructor called");
}
public void onCreate(SQLiteDatabase db)
{
Message.message(context,"on create");
db.execSQL(CREATE_TABLE);
Message.message(context,"table created");
}
public void onUpgrade(SQLiteDatabase db,int oldVersion,int newVersion)
{
Message.message(context,"upgrade called");
db.execSQL(DROP_TABLE);
onCreate(db);
}
ContentValues contentValues;
public SQLiteDatabase db;
public void insertData()
{
contentValues.put(UID,"W");
contentValues.put(UTITLE,"w");
contentValues.put(UPRICE,"10");
contentValues.put(UDESCP, "abc");
db.insert(TABLE_NAME, null, contentValues);
}
}
You haven't initialized your db or contentValues objects. At the start of your insertData method, try putting the following:
db = getWriteableDatabase();
contentValues = new ContentValues();
I have veritabani.java and i create database in this class. Then i create object in mainactivity.java. But when i run app. the program dont move database.java so i cant create database how i can solve this problem.
veritabani.java
public class veritabani extends SQLiteOpenHelper {
private static final String VERİTABANİ_ADİ="kayit";
public veritabani(Context c)
{
super(c,VERİTABANİ_ADİ,null,2);
}
#Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE kayit(ilacadi TEXT);");
}
#Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXIST kayit");
onCreate(db);
}
}
mainactivity.java
public class MainActivity extends Activity {
private veritabani v1;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.home);
v1=new veritabani(this);
}
v1=new veritabani(this) doesnt work.
Try this code
public class DBAdapter
{
private static final String DATABASE_NAME = "GSDATA.db";
private static final int DATABASE_VERSION = 2;
private static final String CREATE_COVERPHOTO_TABLE = "CREATE TABLE IF NOT EXISTS COVERPHOTO (path Text,date DATE);";
private final Context context;
public String group;
private DatabaseHelper DBHelper;
private static SQLiteDatabase db;
public DBAdapter(Context ctx)
{
this.context = ctx;
DBHelper = new DatabaseHelper(context);
}
private class DatabaseHelper extends SQLiteOpenHelper {
DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
#Override
public void onCreate(SQLiteDatabase db)
{
db.execSQL(CREATE_COVERPHOTO_TABLE);
System.out.println("The Database is Created Here :");
}
#Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// db.execSQL("DROP TABLE IF EXISTS calSimpleNote");
onCreate(db);
}
} // database helper class complete
// ---opens the database---
public DBAdapter open() throws SQLException {
db = DBHelper.getWritableDatabase();
return this;
}
// ---closes the database---
public void close() {
DBHelper.close();
db.close();
}
// and write below line in main activity
DBAdapter adapter = new DBAdapter(MainActivity.this);
I'm not sure what you mean by "the program dont move database.java" but i had issues with databases using SQLiteOpenHelper.
Actually, SQLiteOpenHelper calls " public void onCreate(SQLiteDatabase db)" only once, so if you change your tables between 2 launch, it won't be updated.
To solve this you have to manually delete the database by going into parameters / applications / [your_app] / erase data (or something like this) and try again.
Please why do I get the error "SQLiteException: no such table: tabella (code1)???
from LogCat I get: "SQLiteException: no such table: tabella (code1)" when I try to access the database fro the activity.
Here is the SQLiteOpenHelper code:
public class DatabaseHelper extends SQLiteOpenHelper {
private static final String CREATE_DB_SQL = "CREATE TABLE tabella (_id INTEGER PRIMARY KEY AUTOINCREMENT, colonna1 TEXT, colonna2 TEXT, colonna3 TEXT);";
public DatabaseHelper(Context context) {
super(context, "basedati", null, 1);
}
#Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(CREATE_DB_SQL);
ContentValues cv = new ContentValues (3);
cv.put("colonna1", "valore colonna 1");
cv.put("colonna2", "valore colonna 3");
cv.put("colonna3", "valore colonna 3");
db.insert("tabella", "colonna1", cv);
}
#Override
public void onUpgrade(SQLiteDatabase db, int arg1, int arg2) {
db.execSQL("DROP TABLE IF EXISTS tabella");
onCreate(db);
}
}
Here is the Activity code in which I try to open the database:
public class MainActivity extends Activity {
private DatabaseHelper db=null;
private Cursor constantsCursor=null;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
db = new DatabaseHelper(this);
constantsCursor=db.getReadableDatabase().rawQuery("SELECT colonna1 FROM tabella ORDER BY colonna1", null);
}
}
Odds are that you have changed the table after running your app at least once. You cannot simply change the Java String CREATE_DB_SQL, you must also update the SQL schema. The easiest way to do this is to increment the version number:
public DatabaseHelper(Context context) {
super(context, "basedati", null, 2); // Change the number to two
}